似乎无法让 TableModelListener 工作

发布于 2024-12-08 02:10:12 字数 2096 浏览 1 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-12-15 02:10:12

当您更改任何表格单元格的值时,setValueAt 方法将被调用,但不会触发任何事件。

尝试将 fireTableCellUpdated(row, column) 方法调用添加到 setValueAt 方法中,如下所示:

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

请注意,您也可以 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文