-1

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 ?

Moe
  • 470
  • 1
  • 9
  • 26
  • 2
    probably wrong track: typically, neither updateUI nor updateComponentTreeUI are needed in application code. The one exception is switching LAF. To help, we need more context, best an SSCCE – kleopatra Nov 14 '12 at 14:21
  • 1
    those new snippets are far from being an SSCCE ... they don't even contain the part which changes visibility. – kleopatra Nov 14 '12 at 14:36
  • 1
    SSCCE ... SSCCE ... SSCCE ... required. No SSCCE, no help - your decision, of course. – kleopatra Nov 14 '12 at 14:45
  • 2
    You continue to post code that is not relevant. Post a complete running example of how it is not working. – Jakub Zaverka Nov 14 '12 at 14:46
  • 1
    @James Hunter everything here including answers are shots to the dark, post an [SSCCE](http://sscce.org/), – mKorbel Nov 14 '12 at 14:47

3 Answers3

2

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.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • My Jframe is static is this an issue ?, I've tried what you recommend still does not refresh the component. – Moe Nov 14 '12 at 14:20
  • have my doubts: either the setVisible triggers a repaint in itself (no need to duplicate) or a re-layout is needed, then repaint isn't enough. Most probably, updateComponentTree isn't involved in the OPs context (without further details, that's a mere assumption on my part :-) – kleopatra Nov 14 '12 at 14:25
  • @JamesHunter could you provide more code involved? What exactly is compsToExperiment and what is its parent? – Jakub Zaverka Nov 14 '12 at 14:27
2

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();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
1

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256