简体   繁体   中英

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);

}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM