开发者

Can't seem to get the TableModelListener to work

开发者 https://www.devze.com 2023-04-10 03:48 出处:网络
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 i

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开发者_如何学C.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.

  • fireTableCellUpdated(int row, int column)
  • fireTableDataChanged()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号