0

I have created a jtable and at the last column I turned it to a Boolean.class turning the whole column into check boxes now what I want to do is get each each check boxes individually so I can create an if statement to remove rows but when I try I only get the first row or when I try to get the seconded check box I remove two rows instead on that row I have clicked on I tried model.getRowCount and table.getRowCount and its not working.

    table.setModel(new DefaultTableModel(data, col)
        {
            public Class<?> getColumnClass(int column)
            {
                  switch(column)
                    {
                    case 0:
                      return String.class;
                    case 1:
                      return String.class;
                    case 2:
                      return String.class;
                    case 3:
                      return String.class;
                    case 4:
                      return String.class;

                    case 5:
                          return String.class;
                    case 6:
                          return String.class;
                    case 7:
                          return String.class;
                    case 8:
                          return String.class;
                    case 9:
                          return String.class;
                    case 10:
                          return Boolean.class;

                    default:
                      return String.class;
            }

            }

            });

The below statement is the only one that works. Is there a way to get the check boxes individually?

                if(Boolean.TRUE)
                {

                    Submit(0);

                    ((DefaultTableModel)table.getModel()).removeRow(0);

                }
Vimal
  • 411
  • 6
  • 28
jack
  • 1
  • 3
  • 5
    you are aware that that entire first method can be replaced by: return column == 10 ? Boolean.class : String.class; right? – Stultuske Nov 07 '19 at 10:17
  • 2
    Also: this statement: if(Boolean.TRUE) is nonsense. Could you give us one example of a flow, in which this condition would not return true? – Stultuske Nov 07 '19 at 10:18
  • Time to brush up on the [basics](https://docs.oracle.com/javase/tutorial/). – Kayaman Nov 07 '19 at 10:23
  • no Stultuske i didnt know that am still new to java and i change the code to what you said thank you for the help what you said second i dont understand do you mean this – jack Nov 07 '19 at 12:27
  • if(Boolean.class != null) { Submit(0); ((DefaultTableModel)table.getModel()).removeRow(0); } – jack Nov 07 '19 at 12:27
  • what i want to do is get the first check box in my row since they are not named how do i do that? – jack Nov 07 '19 at 12:28
  • @jack You should use proper interpunction. Your comments are hard to read. – MC Emperor Nov 07 '19 at 14:05

1 Answers1

1

Add a TableModelListener to your model, as per https://stackoverflow.com/a/22711419/9569292

When the listener is called, the event will tell you which cell(s) have changed. Check if the value is of type Boolean (or if the column index is 10), then take action on the row where the event originated.

Edit:

Let me tell you what's wrong with the code in your comment:

void Checkbox1() {
    table.getSelectedRow() //useless invocation without an assignment
    if(table.isRowSelected(0)) {
        if(Boolean.class != null) { //this is ALWAYS true
            if(Boolean.valueOf(true)) { //this is ALWAYS true
                table.setRowSelectionAllowed(true);
                table.getSelectedRow(); //useless invocation without an assignment
                Submit(0); //don't know what this does
                ((DefaultTableModel)table.getModel()).removeRow(0);
                errorText.setVisible(false);
            } else {
                if(Boolean.valueOf(false)) { //this is ALWAYS false and will never happen
                    errorText.setVisible(true);
                }
            }
        }
    }
}

I don't know how you tested this to "work", but it needs some... well... work.

When I mentioned TableModelListener I was thinking of something perhaps more like this:

TableModelListener l = new TableModelListener() {
    @Override
    public void tableChanged(TableModelEvent e) {
        if(e.getType()==TableModelEvent.UPDATE && e.getColumn()==10) {
            //only consider updates to Boolean value in col index 10
            //note the reversed order since we are potentially deleting rows
            for(int row=e.getLastRow();row>=e.getFirstRow(); row--) {
                if((boolean)model.getValueAt(row, 10)) { //this should be the Boolean value in the affected row
                    model.removeRow(row);
                }
            }
        }

    }
};
Thomas Timbul
  • 1,634
  • 6
  • 14
  • Thank you Thomas, I think i figure it out it works feel free to check the code – jack Nov 07 '19 at 14:39
  • void Checkbox1(){ if(table.isRowSelected(0)) { if(Boolean.class != null) { if(Boolean.valueOf(true)) { table.setRowSelectionAllowed(true); table.getSelectedRow(); Submit(0); ((DefaultTableModel)table.getModel()).removeRow(0); errorText.setVisible(false); } else { if(Boolean.valueOf(false)) { errorText.setVisible(true); } } } } } – jack Nov 07 '19 at 14:39
  • @jack That will *always* remove the row if `Checkbox1()` is called and row0 is selected. Your `else` is never, ever going to happen. – Thomas Timbul Nov 07 '19 at 15:51
  • okay thank you i deleted the else and i want to always remove the rows because i have a refresh button that adds new rows but i tired your code out and doesnt seem to work but i keep trying it – jack Nov 08 '19 at 08:10
  • Did you add the listener to the model? I would suggest declaring your model as a local variable called `model`, then call `table.setModel(model);` and `model.addListener(l);`. – Thomas Timbul Nov 08 '19 at 11:07