Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I get a button to open another existing panel?  RSS feed

 
Charlotte Benett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm relatively new to Java and I am attempting to create a program which includes three panels. The first panel has three buttons, two of which lead to the other two panels and the third is an exit button.

I managed to figure out how to create the exit button using something like:



I was hoping I would be able to use something similar to this structure, but instead of 'System.exit(0);', maybe I could have something that would prompt the next panel to open and shut the previous one? I have tried searching online for a long time and cannot find anything appropriate that works, so I would really appreciate some help please!

The other suggestion I have come across is to do with booleans and setting the panels to visible or not, but having not yet properly studied this, I'm struggling with how to write it and just keep on getting errors, so if anyone thinks that may be a good method, could they please explain how exactly I would go about writing it please?

I hope I have explained myself well enough - sorry, but I am new to Java and so this may seem really basic and obvious, but it would be great if anyone would be kind enough to help me through it!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at CardLayout.

And welcome to the Ranch!
 
Charlotte Benett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Take a look at CardLayout.

And welcome to the Ranch!

Thank you for the reply and the welcome!

I've had a look at CardLayout, although it seems to put all of the code into one class - I was hoping to have each panel as a seperate class so that it would be easier when they get more complicated with lots of buttons and formatting and things. Is there any way that I could do that please?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charlotte Benett wrote:I've had a look at CardLayout, although it seems to put all of the code into one class

Nothing about CardLayout mandates how many classes you use. If you're talking about example code, most tutorial examples are in the form of an SSCCE that is easy to copy/paste/compile/run for a better understanding of the tutorial topic.
 
Charlotte Benett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Charlotte Benett wrote:I've had a look at CardLayout, although it seems to put all of the code into one class

Nothing about CardLayout mandates how many classes you use. If you're talking about example code, most tutorial examples are in the form of an SSCCE that is easy to copy/paste/compile/run for a better understanding of the tutorial topic.

If that's the case, how would I go about calling another panel from another class to open up from the main panel in the main class? I think this is really the big problem I'm having. I just can't figure out how to connect the classes up...
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charlotte Benett wrote:how would I go about calling another panel from another class to open up from the main panel in the main class?

You don't. You invoke the appropriate method of CardLayout to show the panel of interest.
 
Charlotte Benett
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Charlotte Benett wrote:how would I go about calling another panel from another class to open up from the main panel in the main class?

You don't. You invoke the appropriate method of CardLayout to show the panel of interest.

Thank you for your response and I will definitely look into the method you have suggested. In the end, I spoke to someone I know who suggested creating a seperate static panel in order to call the other panels to it, which seems to work well by adding and replacing panels as buttons are pressed. I have no idea why I couldn't do that without the static panel in the background, but it works, so I'm not going to complain!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!