• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Add JRadioButtons in a JTable

Hi,
How can I add two JRadioButtons ("Yes" and "No")
in one cell of a JTable,
is this possible ?
Thank you
0
matthew016
Asked:
matthew016
  • 6
  • 5
  • 3
  • +2
3 Solutions
 
CEHJCommented:
You'd need a custom cell renderer and editor:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

It would be easier to use a JCheckbox
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
matthew016Author Commented:
Thank u,
I have a last question about this,
I'm using a DefaultTableModel because I want to
dynamically add and delete rows.
But some cells may not be edited,
so I read in some tutorials that I need to use an AbstractTableModel and give it in the JTable constructor
and code the isCellEditable function.
But in the JTable constructor I'm already giving the DefaultTableModel as parameter ...

            model = new DefaultTableModel(items,columnNames());
            table = new JTable(model);

How disable some cell editing then ?
Thank you.
0
 
objectsCommented:
> But in the JTable constructor I'm already giving the DefaultTableModel as parameter ...

doesn't matter, JTable expects a TableModel so a AbstractTableModel subclass will be fine

> so I read in some tutorials that I need to use an AbstractTableModel and give it in the JTable constructor

You can also just subclass DefaultTableModel if you want.
0
 
CEHJCommented:
>>But in the JTable constructor I'm already giving the DefaultTableModel as parameter

You need to override isCellEditable


JTable table = new JTable(new DefaultTableModel(data, columnNames) {
    public boolean isCellEditable(int row, int column) {
        return column != 0; // First column not editable
    }
});
0
 
objectsCommented:
> You need to override isCellEditable

already been mentioned, not sure why you feel you need to repeat comments all the time.
0
 
CEHJCommented:
You clearly didn't read matthew016's last question very well. I took the trouble to do so ;-)
0
 
objectsCommented:
thats strange cause u suggested exactly what I said. go figure :-D
0
 
CEHJCommented:
>>u suggested exactly what I said

I actually explained what matthew016 already partially knew ;-)

>>so I read in some tutorials that I need to ... code the isCellEditable function.
0
 
matthew016Author Commented:
Stop arguing,
no worries object I can see that u gave tip first,
CEHJ's comment was interessant as well even if I cant use this
JTable table = new JTable(new DefaultTableModel(data, columnNames) {
since I want to add rows dynamically I need the reference of the DefaultModel
0
 
CEHJCommented:
>>since I want to add rows dynamically I need the reference of the DefaultModel

That's not a problem

DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {
    public boolean isCellEditable(int row, int column) {
        return column != 0; // First column not editable
    }
};
JTable table = new JTable(dtm) ;
0
 
matthew016Author Commented:
ok I did a class MyDefaultTableModel extends DefaultTableModel, it's ok as well,
Well ... then I'll just sh** up :-)
Thx.
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
no worries :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now