kata
kata

Reputation: 53

Java JTable repaint() method is not working after changing data

When I run it, the table should be empty, but after I click the "Load Data" button I want it to show the data I get from a database. When I check the returned data in this part:

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();
}

it is correct, so there's no problem with that I think. Can someone explain why repaint() isn't working or what am I doing wrong?

Here's my code:

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();
    }

}

Upvotes: 1

Views: 4215

Answers (2)

Matthew Wright
Matthew Wright

Reputation: 1525

It seems to be working for me when I set a value in data. You are changing the object of data with data = cd.getData(); so that the table won't know what you did. Try going through the returned data and update the original array for example:

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

Although will the two arrays have the same size? I would recommend using a TableModel and do this for example:

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]});
}

Upvotes: 2

VGR
VGR

Reputation: 44335

repaint is not the way to do that. You should remove both calls to repaint().

Updating your arrays won't make a difference, because the JTable made a copy of them during construction and is no longer using them at all.

You need to tell your the table's model to use (a copy of) the new data:

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

Upvotes: 2

Related Questions