简体   繁体   中英

Can`t update JFrame

I'm trying to update the frame but it doesn`t happen. How I can do it and what happens when Refresh() calls?

public class tttt extends JFrame{
    private JTable tab;
    private JPanel panel1;

    private void createUIComponents() {//Custom Create for tab
        Object[][] data1 =  {{'1'}};
        Object[] data2 = {'2'};
        tab = new JTable(data1, data2);
    }

    public void CreateFrame(){
        setContentPane(panel1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void Refresh(){
        Object[][] data1 =  {{'9'}};
        Object[] data2 = {'9'};
        this.tab = new JTable(data1, data2);
    }
}

public class Main {
    public static void main(String[] args) {
        tttt Frame = new tttt();//create frame with table with '1'
        Frame.CreateFrame();
        Frame.Refresh();//frame doesn`t change
    }
}

Sorry for my english.

You need to correct many things in your code:

  1. You need to properly create the component hierarchy and instantiate all objects in your component hierarchy. In your example, you should have JFrame -> JPanel -> JTable correctly initialized and added to the component hierarchy. Your code is not instantiating the JPanel named panel1 . You will get a Null pointer exception in this line setContentPane(panel1); .

  2. The pack(); and setVisible(true); methods should be called just before you want to display your UI. They should be called after your Refresh() method. Again, use naming conventions to name your classes and methods correctly.

I hope this helps!

Update:

Posting code:

public class tttt extends JFrame{
    private JTable tab;
    private JPanel panel1;
    private JButton btnAddData;

    private void createUIComponents() {//Custom Create for tab
        Object[][] data1 =  {{'1'}};
        Object[] data2 = {'2'};
        tab = new JTable(data1, data2);
        btnAddData = new JButton("Refresh");

        btnAddData.addActionListener(e->Refresh());

        panel1 = new JPanel(new BorderLayout(10, 10));
        panel1.add(btnAddData, BorderLayout.NORTH);
        panel1.add(tab, BorderLayout.CENTER);
    }

    public void CreateFrame(){
        createUIComponents();
        setContentPane(panel1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void Refresh(){
        Object[][] data1 =  {{'9'}};
        Object[] data2 = {'9'};
        tab.setModel(new DefaultTableModel(data1, data2));
        revalidate();
    }

    public static void main(String[] args) {
        tttt Frame = new tttt();//create frame with table with '1'
        Frame.CreateFrame();
//        Frame.Refresh();//frame doesn`t change

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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