Link to home
Create AccountLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of matthew016

ASKER

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.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
> You need to override isCellEditable

already been mentioned, not sure why you feel you need to repeat comments all the time.
You clearly didn't read matthew016's last question very well. I took the trouble to do so ;-)
thats strange cause u suggested exactly what I said. go figure :-D
>>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.
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
>>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) ;
ok I did a class MyDefaultTableModel extends DefaultTableModel, it's ok as well,
Well ... then I'll just sh** up :-)
Thx.
:-)
no worries :)