0

I am writing a program in Java with Java Swing. I have a class, which is a custom JPanel (my class extends JPanel), which is a log in page. The panel contains a button called "Enter".

When I create my main JFrame, I add the Log in Panel in it. When the button "Enter" is pressed I want to remove the Log In panel and proceed to the next panel.

So how can I make my frame understand when the button "Enter" from the Log In panel is pressed (they are in different classes), so that it proceeds to the next page?

user3309479
  • 329
  • 1
  • 5
  • 17
  • 2
    How? Let me count the ways...seriously, there are so many. You could have the parent class register an `ActionListener` to the `LogIn` panel, but a more robust solution would be to use some kind of MVC, [for example](http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) and [example](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749) – MadProgrammer Feb 04 '17 at 08:23
  • 1
    You should also become farmiluar with the [Observer Pattern](http://www.oodesign.com/observer-pattern.html) – MadProgrammer Feb 04 '17 at 08:23
  • thank you, I was not aware of that. – user3309479 Feb 04 '17 at 08:30

1 Answers1

0

To be able to switch beetween JPanels enclose them in CardLayout:

JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
...
JPanel card2 = new JPanel();
...

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

You should add ActonListener to the "Enter" button:

JButton enterButton = ...
enterButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
              CardLayout cl = (CardLayout)// your reference to the CardLayout here eg. yourJFrame.getContentPane().getLayout();
              cl.show(cards, "The name of panel to show, you gave it with the add operation on cardLayout eg. BUTTONPANEL OR TEXTPANEL");
      }

});
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • 2
    Conceptually, the "action" management is the responsibility of the "panel" and the navigation the responsibility of the "container", so the "panel" needs to let the "container" known when it's job is done and the "container" should then decide what to do about it. This maintains a loose coupling and adherence to the single responsibility paradigm. I only mention it because I have no idea where `enterButton` is in the hierarcy ;) – MadProgrammer Feb 04 '17 at 08:34
  • Yup, but try to elaborate it with simple answer to question: How to switch panels on button click? Be my guest and improve the answer :) – Krzysztof Cichocki Feb 04 '17 at 08:58
  • 2
    [I don't need to](http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) or at least [not again](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749) :P – MadProgrammer Feb 04 '17 at 09:00