Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to access the components of a duplicated JPanel in java?

For example: I want to access or update the button of the duplicated JPanel such as

JPanel secondPanel = new JPanel();
secondPanel=(new MyFirstPanel());
//I tried to access the components using this code
duplicatedPanel.button.setText();

The error is that it cannot find the symbol button1

//This is my full sample code
  class MyFirstPanel extends JPanel{
  JButton button=new JButton();
     public MyFirstPanel() {
     add(button);
     }
  }
//frame
class frame extends JFrame{
frame(){
    JPanel duplicatePanel=new JPanel();
    MyFirstPanel firstPanel=new MyFirstPanel();
    duplicatePanel=(new MyFirstPanel());
    firstPanel.button.setText("Button1");
    duplicatePanel.button.setText("Button2");
    add(firstPanel);
    add(duplicatePanel);
    setVisible(true);
    setLayout(new FlowLayout());
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String []args){
    new frame();
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error was in the duplicatePanel.button.setText("Button2"); "Cannot find the symbol/variable button", is there a way to access this duplicatedPanel button?
the code was working with firstPanel.button.setText("Button1");

>Solution :

I’m not clear why you’re attempting to build this interface in this manner, but this looks like a polymorphism issue.

In your mind, duplicatePanel is a MyFirstPanel, because you assigned it that way in the third line of the frame constructor. When you declared it, however, you only declared it as a JPanel, so when you go to reference the button contained inside, the compiler doesn’t know about the button. The two fastest solutions to your problem would be:

  1. Declare MyFirstPanel duplicatePanel in the beginning, instead of as a JPanel.

or

  1. Cast duplicatePanel as a MyFirstPanel when accessing the button (ie, ((MyFirstPanel)duplicatePanel).button.setText("Button2");).

On a deeper level, I feel like you’re assuming something about the relationship between the two panels that isn’t really there (as far as duplicating — they aren’t clones of each other or anything, they just both have buttons), but hopefully fixing this problem will help you on your way to whatever your final learning destination is.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading