Switch to specific JPanel using Cardlayout

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

Answers


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.


Need Your Help

after submit redirect to the same page

c# asp.net-mvc-4 razorengine

I am using smoothscroll navigation, like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.