Adding CheckBox to DefaultTableModel

I have a DefaultTableModel that is populated with an Object [] [] array.

Now I want to add a column with checkbox and follow the appropriate steps.

When I add a flag to the Object [] [] array and look at it, I get the text displayed

'javax.swing.JCheckBox [, 0,0,0xx ....', how can I show it, to show the flag and add actions to it?

+3
source share
5 answers

JTable has a default checkbox renderer / editor for boolean values. Just return TableModel#getColumnClass Boolean.classfor this column.

+4
source

how can i check the box

See Uhlen answer

and add actions to it?

TableModelListener. - :

public void tableChanged(TableModelEvent e)
{
    if (e.getType() == TableModelEvent.UPDATE)
    {
        int row = e.getFirstRow();
        int column = e.getColumn();

        if (column == ?)
        {
            TableModel model = (TableModel)e.getSource();
            Boolean value = (Boolean)model.getValueAt(row, column));

            if (value.booleanValue())
                // add your code here
        }
    }
}
+2

. :

 //create the table 
DefaultTableModel tableModel = new DefaultTableModel(data, columnNames)
//override the method


               {
        public Class<?> getColumnClass(int colIndex) {

                return getValueAt(0, colIndex).getClass();

            }

, , :

 data[i][12] = new Boolean(false);

:)

+2

, swing []. .

DefaultTableModel , Boolean.

, Boolean.TRUE/Boolean.FALSE . .

0

Source: https://habr.com/ru/post/1777729/


All Articles