Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to can I from one panel access elements on another panel ?

Status
Not open for further replies.

sergeiY

Programmer
Feb 13, 2003
132
AU
I have a JFrame with 2 panels and a JMenu

I am struggling to find a way how I can access JMenu from one of my panels ...

Also how can I access elements on one panel from other panel ? At the moment I am passing a reference from one panel to other in the constructor but it's only one way communication now ... I can't go from second panel to first ...
I am sure there must be an elegant way to solve this problem.

Many thanks.

Sergei
 
Sounds like you're instantiating the components within a method like this:
Code:
class foo extends JFrame
{
   public foo()
   {
      JPanel jpanel1 = new JPanel();
      JButton button1 = new JButton();
      button1.setText("test");
      jpanel1.add( button1 );
      this.getContentPane().add(jpanel1);
   } 
}

If you define all your components as class variables, they will be visible to all other components:
Code:
class foo extends JFrame
{
   JPanel jpanel1 = new JPanel();
   JButton button1 = new JButton();

   public foo()
   {
      button1.setText("test");
      jpanel1.add( button1 );
      this.getContentPane().add(jpanel1);
   } 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top