File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Need help with RadioButtons in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with RadioButtons in JTable" Watch "Need help with RadioButtons in JTable" New topic
Author

Need help with RadioButtons in JTable

Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
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

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
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

Joined: Sep 20, 2005
Posts: 862
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with RadioButtons in JTable