Unable to access JPanel publicly ()

I recently tried to create a simple program that will require you to move the button several times on the screen, but for this I need to have access to the JPanel from certain parts of the code, t seems to be able to make or find an alternative method. Here is a small program that should identify the problems that I have.

public class ButtonMover extends JFrame { public static void main(String[] args) { new ButtonMover(); } JButton actionButton; public ButtonMover() { JPanel buttonMoverPanel = new JPanel(); buttonMoverPanel.setLayout(new GridBagLayout()); this.add(buttonMoverPanel); this.setSize(500,500); this.setResizable(true); this.setVisible(true); JButton actionButton = new JButton("Testing Button"); buttonMoverPanel.add(actionButton); ClickListener c = new ClickListener(); actionButton.addActionListener(c); } private class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == actionButton) buttonMoverPanel.add(new JLabel("Testing Label")); //insert code to move button here } } } 

| buttonMoverPanel.add (new JLabel ("Label Testing")); | the line is the only part that does not work, because I cannot make a link to buttonMoverPanel from this area. Although this does not cause any errors, it prevents the actionButton action.

+4
source share
2 answers

If you need to access a variable, here is your ButtonMoverPanel, then do not hide it by declaring it in a method or constructor, making it visible only in this method or constructor. No, declare it in the class so that it is visible throughout the class.

So, one improvement for this code is to declare buttonMoverPanel in the class just like you are doing with your actionButton JButton right now.

Edit: you are obscuring your actionButton variable - you are re-declaring it in the constructor, so the button added in the GUI is not called the field of the actionButton class. Do not repeat the announcement in the classroom.

In other words, the specified line creates a completely new variable actionButton, which is visible only in the constructor:

 JButton actionButton; JPanel buttonMoverPanel = new JPanel(); public ButtonMover() { buttonMoverPanel.setLayout(new GridBagLayout()); this.add(buttonMoverPanel); this.setSize(500, 500); this.setResizable(true); this.setVisible(true); JButton actionButton = new JButton("Testing Button"); // ****** here 

The solution is to not re-declare the variable rather to use the class field:

 JButton actionButton; JPanel buttonMoverPanel = new JPanel(); public ButtonMover() { buttonMoverPanel.setLayout(new GridBagLayout()); this.add(buttonMoverPanel); this.setSize(500, 500); this.setResizable(true); this.setVisible(true); actionButton = new JButton("Testing Button"); // ****** Note the difference??? 
+5
source

make buttonMoverPanel as a lavel class variable as shown below.

  public class ButtonMover extends JFrame
 {

     public static void main (String [] args) {

         new ButtonMover ();
     }
         JButton actionButton;
         JPanel buttonMoverPanel;

         public ButtonMover () {
             buttonMoverPanel = new JPanel ();
             buttonMoverPanel.setLayout (new GridBagLayout ());
             this.add (buttonMoverPanel);
             this.setSize (500,500);
             this.setResizable (true);
             this.setVisible (true);

             JButton actionButton = new JButton ("Testing Button");
             buttonMoverPanel.add (actionButton);

             ClickListener c = new ClickListener ();

             actionButton.addActionListener (c);

         }

         private class ClickListener
                     implements ActionListener
            {
                public void actionPerformed (ActionEvent e)
                {

                        if (e.getSource () == actionButton)
                             buttonMoverPanel.add (new JLabel ("Testing Label"));
                             // insert code to move button here
                }
            }
 } 
0
source

All Articles