Can I add JRadioButton into JTable

后端 未结 1 1361
不思量自难忘°
不思量自难忘° 2020-11-27 22:44

I tried to add JRadioButton into JTable by using CellEditor and CellRenderer, but I can\'t add JRadioButton

相关标签:
1条回答
  • 2020-11-27 23:40

    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.

    0 讨论(0)
提交回复
热议问题