简体   繁体   中英

How to update Jframe after hiding a Jpanel

I would like to hide selected panel when a click a button. I have most of that working but when a panel is to false doesn't update.

This one of the things I've tried:

compsToExperiment.setVisible(mapVis);
updateUI();


private void updateUI() {
        SwingUtilities.updateComponentTreeUI(this);
    }

is this on the right track? Any other ways of doing it ?

updateComponentTreeUI() doesn't force repaints. You must use the repaint method.

compsToExperiment.setVisible(mapVis);
frame.repaint();

where frame is the top window where the panel resides.

You does not have to do something fancy, other then calling setVisible(false); on the said JPanel . Below example is a proof of that :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InvisiblePanel
{
    private JPanel centerPanel;
    private JPanel footerPanel;

    private JTextField addField;
    private JTextField nameField;
    private JTextField occField;
    private JTextField phoneField;
    private JLabel nameLabel;
    private JLabel addLabel;
    private JLabel occLabel;
    private JLabel phoneLabel;

    private JPanel contentPane;
    private JButton hideButton;

    private void displayGUI() 
    {
        JFrame frame = new JFrame("Invisible Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setBackground(Color.RED.darker().darker());
        contentPane.setLayout(new BorderLayout(5, 5));

        nameLabel = new JLabel("Guarantee Name : ");
        nameField = new JTextField();   
        addLabel = new JLabel("Address : ");
        addField = new JTextField();        
        occLabel = new JLabel("Occupation : ");
        occField = new JTextField();
        phoneLabel = new JLabel("Phone : ");
        phoneField = new JTextField();
        centerPanel = new JPanel();
        hideButton = new JButton("Hide");
        hideButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (centerPanel.isShowing())
                    centerPanel.setVisible(false);
                else
                    centerPanel.setVisible(true);
            }
        });

        centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        centerPanel.setOpaque(true);
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setLayout(new GridLayout(0, 2, 5, 5));
        centerPanel.add(nameLabel);
        centerPanel.add(nameField);
        centerPanel.add(addLabel);
        centerPanel.add(addField);
        centerPanel.add(occLabel);
        centerPanel.add(occField);
        centerPanel.add(phoneLabel);
        centerPanel.add(phoneField);

        footerPanel = new JPanel();
        footerPanel.add(hideButton);

        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    } 

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new InvisiblePanel().displayGUI();
            }
        });
    }
}

You can do JFrame.invalidate() to refresh. Have a look at the link for more details. A similar requirement for a different question. refresh the contents of frame in real time

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