似乎无法让 TableModelListener 工作
我正在创建一个 UI 类,其中所有内容都将运行(不同的类将充当运行程序)。在这个类中,我有一个表,该表应该在更改时创建 TableModeEvents,但它似乎没有这样做。
每当我对表执行任何操作时,控制台都应该打印出“test”,但事实并非如此。我已经制作了其他 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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您更改任何表格单元格的值时,
setValueAt
方法将被调用,但不会触发任何事件。尝试将
fireTableCellUpdated(row, column)
方法调用添加到setValueAt
方法中,如下所示:请注意,您也可以
fireTableDataChanged()
,但是这将触发最一般的事件,建议您触发最具体的事件,因为它可以避免不必要的工作并且能够维持选择状态。fireTableCellUpdated(int 行, int 列)
fireTableDataChanged()< /代码>
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 yoursetValueAt
method, like so: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()