我正在创建一个 UI 类,一切都将在其中运行(另一个类将作为运行器工作)。在这个类中,我有一个表,该表应该在更改时创建 TableModeEvents,但它似乎没有这样做。
每当我对桌子做任何事情时,控制台都应该打印出“测试”,但事实并非如此。我已经制作了其他 SSCCE,只要我将所有内容都保存在一个程序中(在主方法中并且只有子类和匿名类),它们就可以正常工作,但我似乎无法让它跨类工作。
知道我做错了什么吗?
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);
}
}