0

I have made a menu by using Java with Swing and when I browse through the menu, I have used this method:

 backButton.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent event) {
           panela.setVisible(false);
           start();
      }
   });

This buttons go back to the start of the menu when the button is pressed, by setting panela to visibility:false and calling the start method. This works fine, but now I want to open a secoundary window besides the window you're already in. I could imagine something like this: (Setting it to true)

backButton.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent event) {
           panela.setVisible(true);
           start();
      }
   });

But it messes it up and doesn't really open a new window but tries to open a Jpanel in the existing window I think.

How can I write a actionlistener to open a new window besides the window that is already open?

PHPeter
  • 77
  • 1
  • 8
  • 1
    Have you tried using a JFrame? – cangrejo Dec 02 '13 at 13:32
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Dec 02 '13 at 13:33
  • Yeah the class that all my methods (menu etc) are in is extended by JFrame.. Would I then have to create a new class that extends JFrame, and create a window in that Jframe and then call that class from my "backButton" actionlistener? – PHPeter Dec 02 '13 at 13:34
  • 1
    @broncoAbierto I think that is poor advice. If the app. currently has a button that can be clicked on, it is most probably already in a `JFrame`. That being the case, the user's desktop does not need more of them! – Andrew Thompson Dec 02 '13 at 13:34
  • *"..extended by JFrame"* Don't extend frame or other top level containers. Instead create & use an instance of one. – Andrew Thompson Dec 02 '13 at 13:35
  • @AndrewThompson Generally, I wouldn't clutter the desktop with windows either, but maybe the asker has a good reason to do it. While I think it's nice to point out that this might not be the best solution for his application, I think we should also try to provide some help so he can get this specific problem out of the way for now. – cangrejo Dec 02 '13 at 13:41
  • @broncoAbierto *"I think we should also try to provide some help"* 'Shooting them in the foot' is not helping. The 2nd link I posted in my first comment offers alternatives. – Andrew Thompson Dec 02 '13 at 13:43
  • Thanks for the good solution, Bronco can you answer my question about the new class? :) And I am doing a beginner course in java programming which doesn't require too much skill and "good-practise" even though I'd like to always do it the right way to get used to that. The problem is that it is just as important that it is user-friendly, which I find it much more to be, if I can add another window. Thanks again – PHPeter Dec 02 '13 at 13:44
  • - Very inspiring Andrew, but I don't know if I am able to do as the example shows since I'm still very new to java. I am trying to keep it simple since I have to explain how I did it. – PHPeter Dec 02 '13 at 13:48
  • Tip: Add @broncoAbierto (or whoever, the `@` is important) to *notify* them of a new comment. – Andrew Thompson Dec 02 '13 at 13:49
  • @PHPeter, if you are new, than take your time and progress slowly but walking in the right way :) – Sage Dec 02 '13 at 13:51
  • @broncoAbierto should I create a new class that extends the JFrame then? AndrewThompson Thanks ! Sage Yeah that is my plan, but I have to start somewhere ;) – PHPeter Dec 02 '13 at 13:52
  • Maybe JDialog will be enough for you ? – Luke Dec 02 '13 at 13:56
  • 1
    @PHPeter Keep in mind that a JFrame IS a window. What you propose would work. If you create any sort of JFrame (classes inheriting from JFrame too) and set its visible property to true, it will show (you might also want to set its size...). Now that you're beginning, it's good that you just play around with the different components to get a feel of how they work, but whenever you build an actual application which you intend to maintain in the future, you should follow advice like what Andrew pointed out. – cangrejo Dec 02 '13 at 13:57

0 Answers0