Yichuan  Wang
Yichuan Wang

Reputation: 41

How do I update the content of JPanel?

I am a beginner of Java. Now I am implementing this small program. It has a button and a table with some random information. When I click the button, it will update the content of the table.

public class GUI extends JFrame{

private Container pane;
private JPanel topBar;
private JScrollPane tablePane;
private JButton repaint;
private JTable a,b;
private String[] columnNames = {"Name",
            "Age",
            "Major"};

public GUI(){
    pane = getContentPane();
    setLayout(new BorderLayout());
    Handler h = new Handler();
    //Create the button
    repaint = new JButton("Repaint");
    repaint.addActionListener(h);
    //the original table content
    String [][] data = { { "a", "a", "a"},
                   { "b", "b", "b"},
                   { "c", "c", "c" }};
    a = new JTable(data, columnNames);
    tablePane = new JScrollPane(a);

    pane.add(repaint,BorderLayout.NORTH);
    pane.add(tablePane,BorderLayout.SOUTH);
}
private class Handler implements ActionListener {
    public void actionPerformed(ActionEvent event){
        //update the table content
        String [][] data = { { "f", "f", "f"},
                   { "e", "e", "e"},
                   { "z", "z", "z" }};
        b = new JTable(data, columnNames);
        tablePane = new JScrollPane(b);
        pane.add(tablePane,BorderLayout.SOUTH);  
    }
}
}

Now the program doesn't work. The content of the table does not change. Could you tell me how to do that? I heard about a method called repaint(). Should I use this method?

Thank you very much

Upvotes: 1

Views: 701

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Don't create a new instance of JTable, simply change the model of the pre-existing table...

private class Handler implements ActionListener {
    public void actionPerformed(ActionEvent event){
        //update the table content
        String [][] data = { { "f", "f", "f"},
                   { "e", "e", "e"},
                   { "z", "z", "z" }};
        a.setModel(new DefaultTableModel(data, columnNames));
    }
}

Take a look at How to Use Tables for more details. You may also find it helpful to read through Model-View-Controller, which will give a better understanding of the basic premises that Swing is built on.

You should also take a look at Initial Threads and make sure you are creating/modifying your UI only from within the Event Dispatching Thread

public GUI(){
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() { 
            pane = getContentPane();
            setLayout(new BorderLayout());
            Handler h = new Handler();
            //Create the button
            repaint = new JButton("Repaint");
            repaint.addActionListener(h);
            //the original table content
            String [][] data = { { "a", "a", "a"},
                           { "b", "b", "b"},
                           { "c", "c", "c" }};
            a = new JTable(data, columnNames);
            tablePane = new JScrollPane(a);

            pane.add(repaint,BorderLayout.NORTH);
            pane.add(tablePane,BorderLayout.SOUTH);
        }
    });
}

Upvotes: 3

Related Questions