Switch to specific JPanel using Cardlayout - java

I've started working on a Java desktop application using netbeans. I have 7 different screens and to represent them I am using JPanel. One JPanel to represent each of them and one to contain all of them(named as mainPanel), which is inside a JFrame. mainPanel uses Cardlayout for the purpose of switching between screens(JPanels). I built all this interface using netbeans ui widgets i.e. drag drop.
LayOut
JFrame
mainPanel (Jpanel) CardLayout
Child1 (JPanel)
Child2 (JPanel)
.
.
.
.
Childn (Jpanel)
I know that one can switch screens using JPanel.next() and Jpanel.previous. but they can only be used when switching is to be done among consecutive screens i.e. if you have to switch to an immediate neighbour. There's also a method JPanel.show() to go to a specific screen but problem is that it takes a parameter name which is a String you associate when you add it to mainPanel using JPanel.add() function. I've added everything using drag and drop, so I don't know what String gets associated, if it does.
Although it looks very primitive and I've already done it without Cardlayout but this time, Cardlayout is a requirement.
Help will be highly appreciated

This example uses a JComboBox to change cards. The example extends JPanel to add a name, but Component has getName() and setName() methods as an alternative. See also this related answer.

Well... I got the answer.
The thing is, when you add something through interface, code for it is autogenerated which is hidden by default. So, I had to look into the autogenerated code for associated string. by default it is card1, card2, card3 and so on.
The example in answer by trashgod is exactly what I want but not the way i want. It has manually associuated the string in custom Jpanel. But it made me think of looking into the autogenerated code. So, thank You very much :)
Now what I need to do is like
mainPanel.show (gameHome, "Card3");

As an alternative, use a ViewSwitcher
It's better suited for what you are trying to do here.

Related

JPanels one above the other

I want to design a jFrame where there are three jButtons and a set of three jpanels one above the other. When we call a jPanel from the respective jButton , that pane will be displayed. It would appear to the user as if the same portion of the jFrame is displaying the content to be shown on clicking each jButton.But when i am trying to set the jPanels one above the other, they are being shown side by side thus elongating the jFrame horizontally. What should i do to put one jPanel over the other? Any other idea than jPanel which should do the work i intend to do would also be help !!
Your behavior sounds like you are using a FlowLayout. This will not "layer" anything. Instead us a CardLayout, which does exactly what you are trying to accomplish. You call method like show, next, and previous to navigate the panels. See How to Use CardLayout for more details.
Also there are probably hundreds of other examples here on so. Go through the cardlayout questions.
[Tip: navigate the different tabs like "votes" and "frequent" to filter to some of the better posts]
Here's one that uses the show() method to switch between two panels by name.

Netbeans - How to take out a Jpanel from JFrame and make it a class?

I originally made a complex GUI in JFrame Form by the form editor.
There are several Jpanels inside the frame.
Now I am thinking of reusing some of the JPanels in other JFrames.
I know it is possible to copy and paste the JPanel, but what I really want to do is factoring out the JPanel to make it a class. Is it possible to do it in Netbeans?
EDIT:
Thanks for the advices, it was really helpful, but not solving my problem.
I still need to factor out the panel. Say if I have a Panel with several child panels inside. (eg: tabbed panel) I would like to design each of the child panel separately instead of doing them all together in a single class. That's my understanding of OOP. I did not do it at the first place because I was not familiar with netbeans nor I was an good designer. Now I realize I need to reuse some of the child panels. Am I able to factor out the code for each panel and make it a class without redoing the work? I used GridbagLayout.

Cardlayout and objects?

Hi I am doing a small project using forms. Presently I used netbeans but my classes became very complex as all jpanel ie cards within one class which is a frame. I was asked to simplify.
My question is If I put one jpanel and it contents in one class.and make objects.
Can I use cardlayout on these objects? So that cards change within a single frame?
This problem stems from using the NetBeans GUI editor to manage the top-level container and everything in it. Instead, use the approach shown here to manage multiple separate forms that can be used in your frame's layout. See also Card Layout Actions, cited here.

what's the use of a frame , a pane or a panel in swing?

I read that JFrame is made of several panes ..what are panes and why is Jframe made of panes ?
And why there is a JPanel while it seems that the JFrame looks exactly like the JPanel but with a menu bar and a close button so what's the need for a JPanel ? Can anybody explain to me clearly the definition and use of those 3 components ?
There are top level containers such as JFrame. These can serve as the main window in which a GUI is built.
Then there are intermediate level containers. These must be placed in other containers, they cannot exist by themselves. They either help you organize components or they add functionality. A JPanel is a very simple container that helps you to organize other components. While a JSplitPane adds the functionality of having two panes that are variable sized.
When you have a complex GUI you may want to use JPanels to organize various areas of your GUI and then add each of the panels to your JFrame.
In Java the Swing API makes use of the Composite Design Pattern. This means that you can compose very complex objects from other objects and still treat the composite objects the same way as the simple objects. So you can put a JPanel into a JPanel and it still behaves like a JPanel.
Think of it like a tackle box (or sewing kit). It is made of a big container. But rather than put many small objects into this big container and make it difficult to manage later you can place some smaller compartments inside the big box. Then hooks and sinkers etc go in the compartments. Its easier to manage. The big box is the JFrame and the compartments are the JPanels.

Best way of subclassing a JPanel in Swing

I am currently trying to build an expanding panel in Swing (akin the WPF's Expander control) and I'd like to retain the usual methods for manipulating it (i. e. setLayout, add, etc.). Only they should be routed to an embedded panel (the one being shown or hidden).
How would one do that? Overriding every method of JComponent and re-routing that to an embedded JPanel would be cumbersome, but that's the only way I see.
Or should I rather make the embedded panel visible to the outside and force users to use something like ExpanderPanel.getInnerPanel() instead. But then it's no drop-in replacement for JPanel which I think would be nice to have.
Take a look at the JXTaskPane from Swingx project. It already does what you need.
In 1.5(ish) Swing routed a few methods to the content pane in JFrame, JApplet, etc. Whilst there appeared to be some usability benefits for those just starting, it doesn't actually fix the problem. So everyone has to deal with a very strangely behaving API. So my advice is to avoid this approach.
If you have a Container widget which holds a panel you want to show and hide, why not layout your inner panel however you want, then add it to the Container panel, then use static methods against the Container to say
JPanel p = new JPanel();
//do something with the JPanel...
ContainerWidget.setContent(p);
ContainerWidget.expandPanel(p,true);
Would somethign like this work?

Categories