Question

I need some help with my JTable. I am writing a program, wich extracts data from a database into a JTable. The first column should be a editable JCheckBox so I am able to work with the checked (true or false) rows and the data.

I am using a AbstractTableModel(with class extends AbstractTableModel) and override these five methods:

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0;
    }

    @Override
    public Class<?> getColumnClass(int col) {
        if (col == 0) {
            return Boolean.class;
        }
        return super.getColumnClass(col);
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

To display the JTable I use:

JTable table = new JTable();
JScrollPane scrollpane = new JScrollPane();

. . .

table = new JTable(data, header);
table.setModel(this);
scrollpane = new JScrollPane(table);

I read the data with a for loop into the data array. The header array I defined. Basically I need the checked rows to send a mail with the right data in it.

EDIT:

package test;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TestCode extends AbstractTableModel {
    private static final long serialVersionUID = -7051817393770003705L;

    String[] header = {"", "header", "header", "header"};
    Object[][] data = {{new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}};

    public TestCode() {
        JFrame frame = new JFrame();

        JTable table = new JTable(data, header);
        table.setModel(this);
        JScrollPane scrollpane = new JScrollPane(table);

        frame.add(scrollpane);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
    }
    @Override
    public boolean isCellEditable(int row, int col) {
        return col == 0;
    }

    @Override
    public Class<?> getColumnClass(int col) {
        if (col == 0) {
            return Boolean.class;
        }
        return super.getColumnClass(col);
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public static void main(String[] args) {
        TestCode code = new TestCode();
    }
}

This is a short snippet of my code to execute to make it easier for you. I want be able to check the JCheckBoxes at the firt column so I am able to read a true or false boolen from that column.

  • Thank you for help!
Was it helpful?

Solution

You have to override AbstractTableModel#setValueAt

@Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
   //     super.setValueAt(aValue, rowIndex, columnIndex); by default empty implementation is not necesary if direct parent is AbstractTableModel
        data[rowIndex][columnIndex] = aValue; 
        fireTableCellUpdated(rowIndex, columnIndex);// notify listeners
    }

Result.

enter image description here

BTW : Don't use new Boolean(false) instead use Boolean.FALSE

OTHER TIPS

Okay leaving aside the structure of the code (I agree that the creation of the table should not be done in the model and things should be separated out better) the reason this is not working is that your table model does not implement setValueAt(Object value, int row, int column).

So when you click on a cell, that method is called, but your data array is not updated so the value for the cell is always false.

Adding

@Override
public void setValueAt(Object value, int row, int col) {
    data[row][col] = value;
}

to your model means the table behaves as you would expect

Simply add implementation of value setter

    @Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = aValue;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top