Hello Members,

I have two classes(A and B) which extend from a JPanel. These two JPanels are then added to a JFrame in a third class. I would like the program to be such that when I click a certain button in A, A becomes invisible and B becomes visible and when I click a button in B, B becomes invisible and A becomes visible and so on....back and forth.

Following is the code I tried. The program below works only once for the toggle. After that, the program does not proceed further at all. Kindly let me know of my errors.

Thank you!!

public class A extends JPanel
{
  
  private B hello;



  public A()
  {
    button.addActionListerner (new ActionListener() {

       public void actionPeformed(ActionEvent e)
        {
         setVisible (false);
         B.setVisible(true);
        }
     }
     );
  }
}

public class B extends JPanel
{
  
  private A hello;



  public B()
  {
    button.addActionListerner (new ActionListener() {

       public void actionPeformed(ActionEvent e)
        {
         setVisible (false);
         A.setVisible(true);
        }
     }
     );
  }
}

Recommended Answers

All 5 Replies

Where is button defined. Is there more than one?

Hello James,

Yes. Sorry. I have a button_1 in A and button_2 in B.

Thank you!

Hello James,

I was able to fix the toggle. The question I have now is - Is there a way to make an Applet invisible?

Thank you!

Please don't ask unrelated questions in the same thread. Create a new thread with the appropriate title. That is, unless the questions (or answers to those questions) directly cause new problems.

As for your actual question, have you tried using the setVisible(true) method? You may need to call a method after that also to cause the widget to render. Or it might not work for applets at all, but it is listed in the JApplet class doc.

Hello Best,

Thank you!

-sciprog1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.