Alexander Auermaa Alexander Auermaa - 7 months ago 42
Java Question

jRadioButton value in jTable

I have just begun to learn Java. I seem to be stuck on a particular thing I would like Java to do for me.

I would like to have two jRadioButtons working together with a jTable. jRadioButton 1 should display "Yes" while jRadioButton 2 should display "No".

All I get, however, is a type of an error message when trying to add row to my jtable.

Instead of my radiobuttons displaying "yes/no", they display "javax.swing.JToggleButton$ToggleButtonModel@2a93a9f2". Any ideas what this 'error message' means? I have googled but cannot find a thing about it. Thanks in advance.

EDIT:
I added this string:

public String Tapp;


My radiobuttons:

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Tapp = "Yes";
}

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Tapp = "No";
}


and on the "Add" button for my jtable I have this.
My buttongroup is called "bgButtonGroup".

private void bAdd11ActionPerformed(java.awt.event.ActionEvent evt) {
ErrorMessage.setText("");
DefaultTableModel model3;
model3 = (DefaultTableModel) tblProduct11.getModel();
if(!tfChassi.getText().trim().equals("")){
model3.addRow(new Object[]{
tf1.getText(),
tf2.getText(),
tf3.getText(),
cb1.getSelectedItem().toString(),
cb2.getSelectedItem().toString(),
cb3.getSelectedItem().toString(),
cb4.getSelectedItem().toString(),
tf4.getText(),
tf5.getText(),
bgButtonGroup.getSelection()});

Answer

bgButtonGroup.getSelection() this is returning the model of the selected radio button, not the text of it. If you set action command to your radio buttons you can get the text by bgButtonGroup.getSelection().getActionCommand(). For example after create your yesRadioButton set "yes" as action command for it with yesRadioButton.setActionCommand("Yes").

private void bAdd11ActionPerformed(java.awt.event.ActionEvent evt) {                                       
ErrorMessage.setText("");
DefaultTableModel model3;
model3 = (DefaultTableModel) tblProduct11.getModel();
if(!tfChassi.getText().trim().equals("")){
    model3.addRow(new Object[]{
        tf1.getText(),
        tf2.getText(),
        tf3.getText(),
        cb1.getSelectedItem().toString(),
        cb2.getSelectedItem().toString(),
        cb3.getSelectedItem().toString(),
        cb4.getSelectedItem().toString(),
        tf4.getText(),
        tf5.getText(),
        bgButtonGroup.getSelection().getActionCommand()});