** [strong text][2]**
In this JTable for check boxes if I checked
- case 1: "cricket" or "Chess" checked, then I want unchecked "football"
- case 2: "football" checked, then I want unchecked both "cricket" and "Chess"
Please help for checkbox logic.
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.table.*;
public final class JCheckExample extends JPanel {
int trc;
private final String[] columnNames = {"ID", "Name", "cricket","chess", "football"};
private final Object[][] data = {{0,"Ram", false,false,true},
{1,"Robert", true,false,false},
{2,"Gopal", false,true,false},
{3,"Rahim", true,true,false}};
private final TableModel model = new DefaultTableModel(data, columnNames) {
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
private final JTable table = new JTable(model);
private final JScrollPane scrollPane = new JScrollPane(table);
public JCheckExample() {
super(new BorderLayout());
add(scrollPane);
JToggleButton check = new JToggleButton("Game Selection");
check.addActionListener(ae -> {
scrollPane.setVisible(!((JToggleButton)ae.getSource()).isSelected());
scrollPane.revalidate();
});
add(check, BorderLayout.NORTH);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Action Setting");
frame.setSize(375, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JCheckExample());
frame.setVisible(true);
}
}

I suggest that you add a listener to the JTable model. Note that TableModelListener is a functional interface and therefore (since JDK 8) can be implemented via a method reference. With a method reference, you can name the method whatever and use whatever access modifier you like, the method just needs to take the same parameters as the interface method and return the same value. For method
tableChanged(of interfaceTableModelListener) this means that the method needs to returnvoidand take a single parameter of typeTableModelEvent. In the below code, I have named the methodadjustCheckBoxes.Whenever one of the check-boxes in the
JTableis changed, methodadjustCheckBoxeswill be called. The method implements the required logic as described (and understood by me) in your question.Edit
I will heed the advice of @kleopatra, as stated in her comment, and offer an alternative solution that uses a custom
TableModelwhich contains the required check-box logic.In the below code, I have removed the
TableModelListenerand have written classCbModelwhich is a customTableModel. Note the overriddensetValueAtmethod in classCbModel.