Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't seem to get the TableModelListener to work

I am creating an UI class in which everything will run (a different class will work as the runner). In this class I have a table and the table is supposed to create TableModeEvents when changed, but it doesn't seem to do so.

The console is supposed to print out "test" whenever I do anything to the table, but it is not. I've made other SSCCE and they work fine as long as I keep everything in one program (in the main method and with only subclasses and anonymous classes) but I can't seem to get it to work across classes.

Any idea what I'm doing wrong?

package SSCCE;

import java.awt.BorderLayout;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class SSCCE {

static Object[][] data = { {"Abyss", Boolean.FALSE},{"Scepter", Boolean.FALSE},{"FoN", Boolean.FALSE} };
public static void main(String[] args){

    //table model------------------------------------------
    TableModel model = new AbstractTableModel(){
        Object[][] rowData = { {"Abyss", Boolean.FALSE},{"Scepter", Boolean.FALSE},{"FoN", Boolean.FALSE} };
        String[] columnNames = {"Name","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 (true);}
    };


    JTable table = new JTable(model);
    //tableChanged------------------------------------------
    model.addTableModelListener(new TableModelListener(){
        public void tableChanged(TableModelEvent e) {
            System.out.println("test");
        }

    });

    //frame stuff, ignore-----------------------------------
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(table,BorderLayout.CENTER);
    frame.setSize(500,400);
    frame.setLocation(400,200);
    frame.setDefaultCloseOperation(3);
    frame.setVisible(true);

}

}
like image 704
Nacht Avatar asked Dec 12 '22 09:12

Nacht


1 Answers

When you change the value of any of the table cells, the setValueAt method is being called, but there's no event being fired.

Try adding the fireTableCellUpdated(row, column) method call to your setValueAt method, like so:

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

Note that you could also fireTableDataChanged(), but that will fire the most general event, and it's advised that you fire the most specific as it avoids unnecessary work and is capable of maintaining the selection state.

  • fireTableCellUpdated(int row, int column)
  • fireTableDataChanged()
like image 153
Nate W. Avatar answered Dec 31 '22 12:12

Nate W.