Java JTable repaint()方法在更改数据后不起作用



当我运行它时,表应该是空的,但在我单击"加载数据"按钮后,我希望它显示我从数据库中获得的数据。当我检查这部分返回的数据时:

for (i=0; i < data.length; i++){
    for (j=0; j < 4; j++){
        if (data[i][j] != null)
            System.out.print(data[i][j] + "  ");
    }
    if (data[i][j-1] != null)
        System.out.println();
}

它是正确的,所以我认为这没有问题。有人能解释一下repaint()为什么不工作,或者我做错了什么吗?

这是我的代码:

public class UserInterface {
    JFrame frame = new JFrame("User Interface");
    JPanel panel = new JPanel();
    JButton button = new JButton("Load Data");
    JTable table;
    JScrollPane scrollPane;
    String[] columnNames = {"name", "age", "address", "phone number"};
    String[][] data = new String[100][4];
    public UserInterface(){
        frame.getContentPane().setSize(200, 300);
        table = new JTable(data, columnNames);
        scrollPane = new JScrollPane(table);
        panel.add(button);
        panel.add(scrollPane);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
                ConnectDatabase cd = new ConnectDatabase();
                try {
                    data = cd.getData();
                    System.out.println("we got the data from db on the ui.");
                    int i, j;
                    for (i=0; i<data.length; i++){
                        for (j=0; j<4; j++){
                            if (data[i][j] != null)
                                System.out.print(data[i][j] + "  ");
                        }
                        if (data[i][j-1] != null)
                            System.out.println();
                    }
                } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e1) {
                // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                table.repaint();
                scrollPane.repaint();
                System.out.println("table repaint is done.");
            }
        });
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) {
    // TODO Auto-generated method stub
        UserInterface ui = new UserInterface();
    }
}

当我在data中设置值时,它似乎对我有效。您正在使用data = cd.getData();更改数据的对象,这样表就不会知道您做了什么。尝试查看返回的数据并更新原始数组,例如:

String[][] temp = cb.getData();
for (i=0; i<temp.length; i++){
    for (j=0; j<4; j++){
        data[i][j] = temp[i][j];
    }
}

尽管这两个阵列的大小相同吗?我建议使用TableModel,例如:

DefaultTableModel model = new DefaultTableModel(new Object[]{"name", "age", "address", "phone number"},0);
table = new JTable(model);
// Now when you populate the table, you would do this for example:
String[][] temp = cb.getData();
for (i=0; i<temp.length; i++){
    model.addRow(new Object[]{temp[i][0],temp[i][1],temp[i][2],temp[i][3]});
}

repaint不是这样做的方法。您应该删除对repaint()的两个调用。

更新数组不会有什么不同,因为JTable在构建过程中复制了它们,并且根本不再使用它们。

你需要告诉你的表的模型使用(的副本)新数据:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setDataVector(data, columnNames);

最新更新