简体   繁体   English

Java JTable TableModel addRow不变

[英]Java JTable TableModel addRow no change

I have more of a problem than a question and hope you can help me find possible reasons for this. 我的问题比问题多,希望您能帮助我找到可能的原因。

In a very simple JTable , where I use the DefaultTableModel . 在一个非常简单的JTable ,在其中使用DefaultTableModel I add a row programmatically during the runtime like this: 我在运行时以编程方式添加一行,如下所示:

public void Update(String neu) {
    System.out.println(model.getDataVector());

    model.addRow(new String[] {neu});

    System.out.println(model.getDataVector());
}

The System-Prints are only a debugging measure for me to realize more. 系统打印只是我实现更多目标的一种调试措施。 Whenever this method runs, I actually add something to the TableModel (prove due to the out.prints) 每当运行此方法时,我实际上都会向TableModel添加一些内容(由于out.prints的证明)

But the change is not permanent. 但是这种改变不是永久的。 Neither do I see a change in my table, nor is the "new value" part of the TableModel the next time the method Update is called. 下次调用Update方法时,我的表中都没有看到更改, TableModel的“新值”部分也没有看到。

What could be the reason? 可能是什么原因?

Edit1: Sadly I cannot make a code snippet small enough to be posted here and yet contain the error. Edit1:遗憾的是,我无法将代码段缩小得足够小,无法在此处发布,但仍包含错误。 I would highly appreciate it however if you guys could give me some hints in generall what might cause the abovementioned behaviour. 但是,如果你们能给我一些总体提示,可能会导致上述行为的原因,我将不胜感激。

EDIT2: I have build a workaround in my code. EDIT2:我在代码中建立了解决方法。 Somehow the problem seemed to be caused by using two different frames for this operation (one frame to ask for the value and the second frame to use and display this value) 不知何故,问题似乎是由于使用两个不同的帧进行此操作引起的(一个帧要求输入值,第二个帧使用并显示该值)

I replaced one frame with a JDialog and now the TableModel updates correctly. 我用JDialog替换了一帧,现在TableModel正确更新了。 I do not know what the problem with using two frames was, but at least I have my code working again. 我不知道使用两个框架的问题是什么,但至少我的代码可以再次工作。 Thank you for your help! 谢谢您的帮助!

When you change data in tableModel, table is not repainted. 在tableModel中更改数据时,不会重绘表。 Try to call model.fireTableDataChanged(); 尝试调用model.fireTableDataChanged(); after adding new row to table model. 在表模型中添加新行之后。

EDIT: fireTableDataChanged will not solve your problem, because it is called automatically as mentioned in comment below. 编辑: fireTableDataChanged不会解决您的问题,因为它会自动调用,如下面的注释中所述。

Another problem I see is that you have two different fields/data models - mdlUebersetzung and model . 我看到的另一个问题是您有两个不同的字段/数据模型mdlUebersetzungmodel One that you use for loggin, one for addingRow. 一种用于登录,另一种用于添加行。 Is it same instance of TableModel? 它是TableModel的相同实例吗?

I tried to write this simple example using exactly your method and it looks like it works. 我试图使用您的方法编写这个简单的示例,并且看起来确实可行。

Here's my code: 这是我的代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.table.*;

public class MyTable extends JFrame implements ActionListener {

    private JTable table;
    private DefaultTableModel model;

    private JButton addButton; // by clicking the button a new row will be added to the table

    // just create the table and set up the interface
    public MyTable() {
        table = new JTable(0,1); // this creates a new Table with 0 rows and 1 column having a DefaultTableModel
        model = (DefaultTableModel) table.getModel();

        JScrollPane tablePanel = new JScrollPane(table);

        addButton = new JButton("Add row");
        addButton.addActionListener(this);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);

        setLayout(new BorderLayout());
        add(tablePanel);
        add(buttonPanel, BorderLayout.PAGE_END);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == addButton) {
            Update("new Row text");
        }
    }

    public void Update(String neu) {
        System.out.println(model.getDataVector());

        model.addRow(new String[] {neu});

        System.out.println(model.getDataVector());
    }

    public static void main(String[] args){
        new MyTable();
    }
}

The thing I noticed is that if I create the JTable using the constructor with no arguments, ie new JTable() , the rows added to the model are all empty (which can be noticed looking at the prints in the terminal), even if I'm passing new String[] {neu} as argument. 我注意到的事情是,如果我使用不带参数的构造函数创建JTable,即new JTable() ,则即使我将添加到模型中的行都清空了(可以在终端中查看打印内容)正在传递new String[] {neu}作为参数。 This is weird because according to the documentation that constructor should return a JTable having a "default data model", which I guess is obtained by calling the createDefaultDataModel() method of JTable, whose documentation says in turn that by default it returns a DefaultTableModel . 这很奇怪,因为根据文档 ,构造函数应返回具有“默认数据模型”的JTable,我猜这是通过调用JTable的createDefaultDataModel()方法获得的,该文档的文档又说默认情况下它将返回DefaultTableModel

EDIT: I noticed now that the constructor for DefaultTableModel with no arguments builds a table model with "zero columns and zero rows". 编辑:我现在注意到没有参数的DefaultTableModel的构造函数将构建具有“零列和零行”的表模型。

As noted by peeskillet in a comment, always remember to add the action listener to the buttons to actually make them do something! 正如peeskillet在评论中指出的那样,请始终记住将动作侦听器添加到按钮上,以使它们实际执行某些操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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