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
LVL 9
matthew016Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.