Link to home
Start Free TrialLog in
Avatar of riaancornelius
riaancornelius

asked on

Some more help with JRadioButtons in a JTable

Seems I closed this question prematurely:
https://www.experts-exchange.com/questions/21921480/JRadioButtons-in-a-JTable.html

Basically, I have that working as it should, Except for the fact that I can't seem to figure out which JRadioButton is actually selected... I have only two radiobuttons, andI'm trying to determine whether the first one is selected:

I have tried both:
tblData.getCellEditor(i,
                        tblData.getColumn("Type").getModelIndex()).
                        getCellEditorValue().toString().equals("0");

model.getValueAt(i, tblData.getColumn("Type").getModelIndex()).
                        toString().equals("0");
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

   Hi!

Take a look at this example http://www.java2s.com/Code/Java/Swing-JFC/RadioButtonDemo.htm
Hope it helps.

Regards,
  Tomas Helgi
Hi TomasHelgi

why don't you simply have a reference to the RadioButtonPanel in your class and then get this reference? Something like the following:

RadioButtonPanel panel = new RadioButtonPanel(answer);
table.getColumn("Answer").setCellEditor(new RadioButtonEditor(panel));

public RadioButtonPanel getPanel()
{
   return panel;
}

Cheers
Then if you have the panel you can work out wihich jradiobuttons are selected.
Avatar of riaancornelius
riaancornelius

ASKER

Hi Girionis, The problem is that I need a different panel for each row, Don't I? I'm getting really confused about what is actually happening in this JTable.

Can somebody please explain in laymen terms what happens in the example I used in the above mentioned question?
why exactly do you need to know which radio button is selected?

> Can somebody please explain in laymen terms what happens in the example I used in the above mentioned question?

when the row is editted the column is 'replaced' with the panel allowing the user to select a radio button.
riaancornelius,
>  Don't I?

Not sure. I think the RadioButtonPanel takes the whole column.
>> why exactly do you need to know which radio button is selected?
I have a table with a choice between two options. After the user makes the selection for each row and hits the next button, I need to know which option was selected for each row, so that I can process each row differently based on the option.

The way I understand it, this editor will change the underlying Integer columns value to the radiobuttons index that is selected, so that if I call table.getValueAt(row,col) it should return index of the radioButton?
thats right, so is getValueAt() returning the correct value?
Guys, getValueAt() returns the right value, but that value is only updated once the cell loses focus.

In my app I was editing the cell, and then clicking on the next button, so the cell didn't lose fucus, and hence the value wasn't updated.
So now the question is, How do I get the cellEditor to update the value the moment you click on a Radiobutton?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks objects :)