• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change one JPanel with other?  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my application I need to change one specific JPanel with another one. But I have more panels in the frame. I would like to keep those panels, and only change that specific panel. I found an example on the Internet. Based on that example I tried to accomplish the same, but failed. Do you know what I need to do to swap the pannel?



What I was hoping for was that the second red panel (secondRed) was exchanged with the second green panel (secondGreen). What I got was that the swapper button was (sometimes) removed (and not added).
 
Ralph van Delft
Greenhorn
Posts: 11
Angular Framework Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using and
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph van Delft wrote:Try using and

Minor correction. It should be (in that order)


Daan Heuvelbeuk wrote:For my application I need to change one specific JPanel with another one.

Looks like a requirement for CardLayout
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use null layouts. Don't use setBounds().

Swing was designed to be used with layout managers.
 
Ralph van Delft
Greenhorn
Posts: 11
Angular Framework Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Minor correction. It should be (in that order)


Daan Heuvelbeuk wrote:For my application I need to change one specific JPanel with another one.

Looks like a requirement for CardLayout
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html




Sorry my bad.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Don't use null layouts. Don't use setBounds().

Swing was designed to be used with layout managers.


Your absolutely right. But the use of the layout was not the topic of my question. To make my problem visible I used what I used. I did not want to have to choose the appropriate layout manager for the situation as it was not part of my problem. It was the swapping.

Maneesh Godbole wrote:
Daan Heuvelbeuk wrote:For my application I need to change one specific JPanel with another one.

Looks like a requirement for CardLayout
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html


I only now see the existence of the tabbed pane (or the combo boxes to swap between different panels). I will need to ponder about that feature. Thanks

Ralph van Delft wrote:Try using and


Neither
nor
work.

If you run the code nothing happens (sometimes the button vanishes, sometimes not)

Edit: Actually, Maneesh, you where absolutely right. It works now. Thanks.

(But I actually would like to know what to do to replace one panel with another one. For a next life or so, but I will set this question to resolved)
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the use of the layout was not the topic of my question. To make my problem visible I used what I used. I did not want to have to choose the appropriate layout manager for the situation as it was not part of my problem. It was the swapping.


Again, until the problem is solved, you don't know what is or isn't relevant to the problem.

More often that not, using a null layout will cause problems, not solve problems. You don't know if using a null layout may have introduced a second problem.

If you real application uses layouts then use layouts. It is not any more difficult to create a SSCCE using a layout. In fact it is easier.

But this SSCCE was much better than your others

But I actually would like to know what to do to replace one panel with another one.


The solution is to use a CardLayout.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!