Switch to a specific JPanel using Cardlayout

I started working with a Java desktop application using netbeans. I have 7 different screens, and I use JPanel to represent them. One JPanel represents each of them and one to contain all of them (called mainPanel), which is inside the JFrame. mainPanel uses Cardlayout to switch between screens (JPanels). I built this whole interface using netbeans ui widgets, i.e. Drag and drop.

Markup

 JFrame mainPanel (Jpanel) CardLayout Child1 (JPanel) Child2 (JPanel) . . . . Childn (Jpanel) 

I know that you can switch screens using JPanel.next() and Jpanel.previous . but they can only be used when switching between successive screens, that is, if you need to switch to the nearest neighbor. There is also a way for JPanel.show() to go to a specific screen, but the problem is that it accepts the name parameter, which is the String that you are binding by adding it to mainPanel using the JPanel.add() function. I added everything using drag and drop, so I don’t know what String binds, if that is the case. Although it looks very primitive, and I already did it without Cardlayout , but this time Cardlayout is a requirement.

Help would be greatly appreciated

+4
source share
3 answers

Ok ... I got a response. The fact is that when you add something through the interface, the code for it is automatically generated, which is hidden by default. So, I had to study the autogenerated code for the related string. the default is card1, card2, card3, etc.

The trashgod answer example is exactly what I want, but not the way I want. He manually linked the string in a custom Jpanel. But it made me think about how to look into auto-generated code. So thank you very much :)

Now i need to do how

  mainPanel.show (gameHome, "Card3"); 
+5
source

This uses JComboBox to change maps. The example adds JPanel to add the name, but Component has the getName() and setName() methods as an alternative. See Also this related answer .

image

+6
source

Alternatively use ViewSwitcher

This is better suited for what you are trying to do here.

+3
source

Source: https://habr.com/ru/post/1433345/


All Articles