Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with RadioButtons in JTable

 
Arun Bommannavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make radio buttons in one row and one column in a JTable, the rest being either Strings or data (double).

For example,


So it looks like a matrix. I wish to select either a row of data or column of data depending on the radio button selected.

Any hints, suggestions are most appreciated.

Regards
Arun
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Bommannavar:
I need to make radio buttons in one row and one column in a JTable, the rest being either Strings or data (double).

For example,


So it looks like a matrix. I wish to select either a row of data or column of data depending on the radio button selected.

Any hints, suggestions are most appreciated.


Don't put instances of JRadioButton() in your table model.
Instead, put either Boolean.TRUE or Boolean.FALSE in your model,
then worry about table cell renderers at a later step.

Also, don't plan on using the cell renderers (even if they look
like radio buttons) to enforce the "only one on" policy. Handle
this in the setValueAt() method of your table model (and be sure
to call fireTableCellUpdated() or fireTableRowsUpdated() methods
for any cells that change value).

As for the renderers, I recommend starting with the default renderer
for Boolean, which will look like a JCheckBox, not a JRadioButton.
(You can change it to a radio button renderer later, after you have
things working.) You can get this for the column of buttons by simply
returning Boolean.class in your table model's getColumnClass() method. Getting a single row to use the rendeder will be a bit harder,
so start with the column.
 
Arun Bommannavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:


Don't put instances of JRadioButton() in your table model.
Instead, put either Boolean.TRUE or Boolean.FALSE in your model,
then worry about table cell renderers at a later step.


Here is what works and what doesn't.


Following code Works :
mp is my TableModel that extends DefaultTableModel



Following code also works:





But (and more importantly) the following code doesn't.





Appreciate your comments and suggstions.

Regards
Arun
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Bommannavar:


Here is what works and what doesn't.

<snip>

But (and more importantly) the following code doesn't.


In what way doesn't it work?

I might suggest that it might be a easier to extend AbstractTableModel
instead of DefaultTableModel. That way you can keep the boolean arrays
and the double arrays separate, referring to them as appropriate in the
getValueAt() method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic