How can you create a JTable with 2 Columns with Checkbox (Boolean) {Yes and No}

1

and that are validated so that if one is selected, the other one is not selected?

Although I do not know if it is better with the RadioButtom for unique selections both work the same as I understand, however in the NetBeans Graphical environment I see that the Buttom Group exists ... however it is not used

Also in the graphic mode (form mode of netbeans forms) it appears in the options to configure the tables the option to create boolean type columns directly .. However, how can these be validated so that once the other one is selected the other one can not be selected anymore, or if one is selected the other will be deselected .. I will look for some images to see if I can understand better:

Place 2 tables in the window for the question of how the radiobutton or the checkboxx or the combobox can be placed inside the table: S but the main question is the first .. haha

    
asked by HeckDan 31.05.2017 в 22:42
source

1 answer

0

There are two options: the first and simplest is to create a TableModel inherited from AbstractTableModel where you return a Boolean in the method getColumnClass (int col) assuming that col is the column where the jCheckBox will appear. The other "old school" option is to create a TableCellRenderer by implementing the TableCellRenderer interface that inherits the component you want (JRadioButton or JCheckbox) and set that renderer to the JTable TableColumn where you want that component to be drawn. For the specific case of the joint validation you will need to use the second option and validate in the method getTableCellRendererComponent (arguments).
An example of the second case would be:

    private class MiJCheckBoxCellRenderer extends JCheckBox implements TableCellRenderer{

          public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column) {

          //Aqui iran tus validaciones que, segun corresponda, haran this.setSelected(true o false);    
          return this;
    }
}

Then in your JTable you will obtain the desired columns (for this case, column 2) and assign them your new CellRenderer in this way:

miJtable.getColumnModel().getColumn(2).setCellRenderer(new PruebaCellRenderer());
    
answered by 01.06.2017 в 03:18