简体   繁体   English

Java swing jTable未更新

[英]Java swing jTable not being updated

I built a jTable using NetBeans GUI, and I want to update it inside the constructor of the class. 我使用NetBeans GUI构建了jTable,我想在类的构造函数中对其进行更新。 I'm planning to add a search option on the frame so the whole update idea is quite critical for me. 我打算在框架上添加搜索选项,因此整个更新思路对我来说非常关键。

My code: 我的代码:

    public availableTrumps(TrumpistClient TC){
    initComponents();
    availableTrumpsTrumpistClient=TC;
    String result=null;

    String query="SELECT * FROM APP.TRUMPS";

        result=this.availableTrumpsTrumpistClient.WritingReading("sql_select", query);

        if (result.contains("empty")){
            JOptionPane.showMessageDialog(this, "There are now trumps to show.");
        }
        else if (result.contains("error")){
            JOptionPane.showMessageDialog(this, "Error in the connection. Please try again.");
        }
        else{
            int i;
            String []data = result.split("\r\n");
            String [][] data2 = new String [data.length][];
            for (i = 0; i < data.length; i++)
            {
                data2[i] = data[i].split("&");
            }
            String[] columnNames = {"From", "To", "Departure Time", "Remaining Places", "Proposer", "ClosingTime", "Cost Per Seat" };
            this.jTable1 = new JTable(data2,columnNames);
            this.jTable1.setPreferredScrollableViewportSize(new Dimension(500,100));
            this.jTable1.setFillsViewportHeight(true);
            JScrollPane jps = new JScrollPane(jTable1);
            add(jps);
            jTable1.revalidate();

    }

    }

The input two-dimentional array data2 is fine and validated. 输入的二维数组data2很好并且已验证。 I added the last 5 rows of the code to see if they help with something. 我添加了代码的最后5行,以查看它们是否有帮助。 I don't know if they are mandatory and in any case I do not want to change the graphical properties of the jTable I built with the GUI (just the data in it). 我不知道它们是否是必需的,无论如何我都不想更改使用GUI构建的jTable的图形属性(仅更改其中的数据)。 When I run the program, I see that the jTable remains empty. 当我运行程序时,我看到jTable保持为空。 Why? 为什么?

I suggest you use a table model, whenever the data changes you change the model. 我建议您使用表模型,只要数据更改,就可以更改模型。 Build the JTable instance only once, not whenever you need to change the data. 仅一次构建JTable实例,而不必在每次需要更改数据时构建。

As others have said, you don't want to create multiple JTable instances. 正如其他人所说,你希望创建多个JTable实例。 Create one like this: 创建一个这样的:

DefaultTableModel model = new DefaultTableModel(new Object[0][0], 
    new String[]{"From", "To", "etc."});

JTable table = new JTable(model);

Then, when you need to add rows, use 然后,当您需要添加行时,请使用

model.addRow(dataForThisRow); // Object

If you want to change a cell: 如果要更改单元格:

model.setValueAt(newValue, row, col); // Object, int, int

Or, to remove row i : 或者,删除第i行:

model.removeRow(i); // int

For more information, see the DefaultTableModel documentation . 有关更多信息,请参见DefaultTableModel文档


If, for some reason, it is imperative that you recreate the table each time, I believe the problem is that you are calling revalidate without calling repaint . 如果出于某种原因每次都必须重新创建表,我相信问题是您在调用revalidate而不调用repaint

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

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