1

When a JPanel is set to not visible is it still "touchable"? As in I have a JPanel on my frame and the panel has buttons for instance. If I set the panel to NOT be visible if I press on where a button is if it were visible, would the button still function?

I am asking to better understand the setVisible not actually trying to achieve what is being said above.

Aly Abed
  • 73
  • 1
  • 9
  • 1
    Why haven't you tested it yourself? – Flown Jul 30 '16 at 12:20
  • Possible duplicate of [JPanel which one of Listeners is proper for visibility is changed](http://stackoverflow.com/questions/10880326/jpanel-which-one-of-listeners-is-proper-for-visibility-is-changed) – SkyWalker Jul 30 '16 at 12:27
  • @Flown I understand why you would say that. I did attempt to test it a couple times, and I just wanted to reaffirm the results I had. Marvin Jude's answer allowed me to make sure the functionality of the panel would not be enabled with the panel being not visible. – Aly Abed Jul 30 '16 at 12:58

2 Answers2

2

It still works when it is set to not visible, but If you don't want it to perform the function for which it was made then make use of the .Enabled method.

1

I don't know whether you have tested what @Marvin Jude said. But for my example, the instance is untouchable when its parent container is invisible.

  1. Use panel.setVisible(true); or panel.setVisible(true); to change the visibility of JPanel.
  2. The button's listener is not triggered if MainPanel is not visible.

See code below.

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;

    public class MyFrame extends JFrame{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public MyFrame(){
            MainPanel panel = new MainPanel();
            add(panel,BorderLayout.CENTER);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            pack();
            panel.setVisible(true);
        }

        class MainPanel extends JPanel{

            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            public MainPanel(){
                JButton button = new JButton("I am a button");
                add(button);
                button.addActionListener(new ActionListener(){

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        System.out.println("JButton is clicked...");
                    }
                });
            }
        }

        public static void main(String args[]){
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    MyFrame myFrame = new MyFrame();
                    myFrame.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }
Eugene
  • 10,627
  • 5
  • 49
  • 67
  • Yes I did test it, and it does work. I kind of used his as a redundancy measure. As for what you have said it, now I understand how it works a little more. Thank you! – Aly Abed Jul 30 '16 at 13:02