Can I add JRadioButton into JTable
Asked Answered
G

1

5

I tried to add JRadioButton into JTable by using CellEditor and CellRenderer, but I can't add JRadioButton into JTable. I am using NetBeans and backend MySQL. Please help me.

Edit: Thank you, but I have no idea about how to group JRadioButton. Can you help me? I have 4 columns .First column cell containing item name , second column cell containing quantity,3rd and 4th column cells contiaining JRadio Buttons.Then I want to grouping 3rd and 4th column cells containing JRadio Buttons in each row

Edit:If I try to add radiobutton in the customize code of jTable by using this,

new JRadioButton("a") , then it'l come as, javax.swing.JRadioButton[,0,0,0x0,invalid,alignmentX=0.0,.....text=a], in the column instead of button

Gilder answered 22/6, 2012 at 10:22 Comment(2)
For better guidance, Please edit your question to include an sscce that exhibits any problems you have.Ochoa
See also this related question.Ochoa
O
6

It's not clear how you want to use JRadioButton in a JTable; consider these alternatives:

  • Use SINGLE_SELECTION mode to select individual rows.

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
  • Use a column of type Boolean.class, which will be rendered using a JCheckBox. This example limits selections to a single row.

  • Use a JComboBox as an editor for mutually exclusive choices within a row.

  • Addendum: If JRadioButton is required, they can be added to a JPanel, as shown in this example due to @mKorbel.

  • Addendum: If each JRadioButton has its own column, you can't use a ButtonGroup because a single button is used for all cells having the same renderer. You can update other button(s) in the same row from your TableModel, which should override setValueAt() to enforce the single-selection rule that is usually managed by the ButtonGroup. There's an example here.

  • Addendum: This example due to @Guillaume Polet illustrates a way to manage one radio button per row.

Ochoa answered 23/6, 2012 at 22:47 Comment(6)
I've migrated your new code to your question and update my answer to include a related example.Ochoa
I have 4 columns .First column cell containing item name , second column cell containing quantity,3rd and 4th column cells contiaining JRadio Buttons.Then I want to grouping 3rd and 4th column cells containing JRadio Buttons in each row.Gilder
Please edit your question to include an sscce that shows you approach; more above.Ochoa
You've un-accepted and immediately re-accepted this answer twice in the past two days. Is everything OK? An sscce will help identify any remaining problems.Ochoa
i have some problems remaining.Gilder
The text you show is returned by JRadioButton#toString(), suggesting that your renderer or your model are incorrect. Absent an sscce, I can only guess.Ochoa

© 2022 - 2024 — McMap. All rights reserved.