0

I have a JTable of 5 columns and 20 rows. The headers are Name, ID, Present, Absent, Late.

I would like to restrict the user from being able to tick all 3 boxes as obviously that wouldn't make sense.

What is the simplest way of doing this so that if for example, present is ticked, then absent is ticked the present value would change to false.

Here is a basic version of my code. lol please ignore how bad it is i'm fairly new to programming.

JPanel classRegPanel = new JPanel(null); //layout

Object data[][]= new Object[10][5];  
String columns[]={"Name","ID","Present", "Absent", "Late"};  

DefaultTableModel model = new DefaultTableModel(data, columns) {

boolean[] canEdit = new boolean[]{
                false, false, true, true,true
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }

@Override
public Class<?> getColumnClass(int columnIndex)
{
    return columnClass[columnIndex];
}};

JTable table=new JTable(model);  

JScrollPane scrollPane=new JScrollPane(table);    

final Class[] columnClass = new Class[] 
{
String.class, Integer.class, Boolean.class, Boolean.class,Boolean.class
};


public void Setup()
 {
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

this.add(scrollPane);

this.setTitle("Register");
this.setSize(500,380);
this.setVisible(true);
this.setResizable(false);
 }

public static void main(String[] args )
 {
    ClassRegister cr = new ClassRegister();
    cr.Setup();
 }
67705151
  • 1
  • 3

1 Answers1

0

Checkboxes allow for multiple selections by design. If you only want ONE seelcted at a time, use radio buttons instead of checkboxes, this is exactly what they are for. see: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio

Patrick Schaefer
  • 821
  • 10
  • 20
  • 1
    (1-) that link has nothing to do with Swing. The columns of data in a JTable are not real components. You can't just treat the data as radio buttons. – camickr Dec 02 '16 at 16:10