2
class TableModel extends AbstractTableModel {

        Object rowData[][] = DataAccess.getSentences();
        String columnNames[] = {"Category", "Sentences", "Boolean"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }

        public void setValueAt(Object value, int row, int column) {
            rowData[row][column] = value;
        }

        public boolean isCellEditable(int row, int column) {
            return (column >= 2);
        }
    }

Sample scenario:

Headers: Category, Sentences, Boolean

Row1: apple | this is an apple | checkbox

Row2: cat | this is a cat | checkbox

When I ticked the first row. It will System.out.println() the "apple" and "this is an apple."

If you have much time, i would appreciate if there's a code snippet. Thank you.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jong
  • 131
  • 1
  • 2
  • 12

3 Answers3

2

JTable get cehckbox value when check box is checked:-

table.getModel().addTableModelListener(new TableModelListener() {
              @Override
              public void tableChanged(TableModelEvent e) {
                   for(int i=0;i<table.getModel().getRowCount();i++)
                      {
                        if ((Boolean) table.getModel().getValueAt(i,0))
                        {  
                          System.out.println(">\t"+table.getSelectedRow());
                          break;
                        }
                     }     
                  }
        });
Krishnakant Kadam
  • 3,225
  • 1
  • 14
  • 9
1

CheckABunch is an example that may get you started:

  • If getColumnClass() returns Boolean.class for a column, the default renderer and editor will be a JCheckBox.

  • For AbstractTableModel, your implementation of setValueAt() must fire the appropriate event, as shown here and here.

  • Add a TableModelListener to receive events fired by your model, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

To get the table entire row data, add the below addTableModelListener for JTable.

table.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                for (int i = 0; i < table.getModel().getRowCount(); i++) {
                    if ((Boolean) table.getModel().getValueAt(i, 0)) {

                        if (table.getSelectedRow() != -1) {
                            int column = 1;
                            int row = table.getSelectedRow();
                            System.out.println("to get one cell Data:\t"
                                    + table.getModel().getValueAt(row, column).toString());
                            System.out.println("To get entire row data:\t"
                                    + model.getDataVector().elementAt(table.getSelectedRow()));
                            break;
                        }
                    }
                }
            }
        });
Rohinibabu
  • 660
  • 10
  • 16