Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to switch between different JPanels using CardLayout and references

Posted on 2009-03-29
6
Medium Priority
?
2,164 Views
Last Modified: 2013-12-14
I have created a Java Desktop Application using NetBeans.  Currently I have all the different panels I am using in the TestView.java and I am able to switch panels easily.  Unfortunitly I know this isn't a good thing to do so I have been playing around with some suggestions I received.  I've tried to set up the program so that there is a control Panel which is in charge of switching the panels but it is not working. When I call the displayImage method the TestView.java file it displays the System Admin screen, which is correct, but when on the sys admin screen when I click the 'Create User' button the create user panel is not displayed.  What am I doing wrong?
public class TestView extends FrameView {
 
    //String password;
    char password[];
   CardLayout cards = new CardLayout();
 
   controlPanel controlPanel = new controlPanel();
 
   systemAdminPanel systemAdminPanel = new systemAdminPanel();
 
 
 
 
    public TestView(SingleFrameApplication app) {
        super(app);
 
        initComponents();
 
         setComponent(controlPanel);
         controlPanel.displayImage("systemAdminWelcome");
   
....}
 
public class controlPanel extends javax.swing.JPanel {
    systemAdminPanel systemAdminPanel = new systemAdminPanel();
 
   createUserPanel newUserPanel = new createUserPanel();
 
   private CardLayout cards = new CardLayout();
 
    /** Creates new form controlPanel */
    public controlPanel() {
        initComponents();
 
        setLayout(cards);
        add(systemAdminPanel, "systemAdminWelcome");
        add(newUserPanel, "newUserForm");
    }
    public void displayImage(String passedChoice) {
		System.out.println("passed choice = " + passedChoice);
		cards.show(this, passedChoice);
	}
}
 
public class systemAdminPanel extends javax.swing.JPanel {
   
    /** Creates new form systemAdminPanel */
    public systemAdminPanel() {
        initComponents();
 
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */                 
 
    private void createUserButtonMouseClicked(java.awt.event.MouseEvent evt) {                                              
        // TODO add your handling code here:
controlPanel controlPanel = new controlPanel();
        controlPanel.displayImage("newUserForm");
    }

Open in new window

0
Comment
Question by:mtnrunner2001a
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24014821
you're creating a new controlpanel and calling it instead of the one in your gui

public class controlPanel extends javax.swing.JPanel {
    systemAdminPanel systemAdminPanel = new systemAdminPanel(this);
 
   createUserPanel newUserPanel = new createUserPanel();
 
   private CardLayout cards = new CardLayout();
 
    /** Creates new form controlPanel */
    public controlPanel() {
        initComponents();
 
        setLayout(cards);
        add(systemAdminPanel, "systemAdminWelcome");
        add(newUserPanel, "newUserForm");
    }
    public void displayImage(String passedChoice) {
                System.out.println("passed choice = " + passedChoice);
                cards.show(this, passedChoice);
        }
}
 
public class systemAdminPanel extends javax.swing.JPanel {
   
   controlPanel controlPanel = new controlPanel();

    /** Creates new form systemAdminPanel */
    public systemAdminPanel(controlPanel controlPanel) {
        this.controlPanel = controlPanel;
        initComponents();
 
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */                
 
    private void createUserButtonMouseClicked(java.awt.event.MouseEvent evt) {                                              
        // TODO add your handling code here:
        controlPanel.displayImage("newUserForm");
    }

0
 

Author Comment

by:mtnrunner2001a
ID: 24015084
I made the changes you suggested and when I try to run I get a 'Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sun.awt.AppContext.get(AppContext.java:580)'.  I think I understand why because I end up in a loop between initializing the systemAdminPanel and the controlPanel but how do I fix it? '
0
 
LVL 92

Expert Comment

by:objects
ID: 24015088
can you post your code

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:mtnrunner2001a
ID: 24015113
Here is my code:

public class TestView extends FrameView {

    //String password;
    char password[];
   CardLayout cards = new CardLayout();

   controlPanel controlPanel = new controlPanel();

   //systemAdminPanel systemAdminPanel = new systemAdminPanel(controlPanel);

    public TestView(SingleFrameApplication app) {
        super(app);

        initComponents();

         setComponent(controlPanel);
         controlPanel.displayImage("systemAdminWelcome");
   ...}


public class controlPanel extends javax.swing.JPanel  {

   
   systemAdminPanel systemAdminPanel = new systemAdminPanel(this);

   createUserPanel newUserPanel = new createUserPanel();

   private CardLayout cards = new CardLayout();



    /** Creates new form controlPanel */
    public controlPanel() {
        initComponents();
 
        setLayout(cards);
        add(systemAdminPanel, "systemAdminWelcome");
     //   add(newUserPanel, "newUserForm");
     
    }

public class systemAdminPanel extends javax.swing.JPanel {

    controlPanel controlPanel = new controlPanel();

    /** Creates new form systemAdminPanel */
    public systemAdminPanel(controlPanel controlPanel) {
        this.controlPanel = controlPanel;
        initComponents();

    }

    private void createUserButtonMouseClicked(java.awt.event.MouseEvent evt) {                                              
        // TODO add your handling code here:
        controlPanel.displayImage("newUserForm");
    }  
}

0
 
LVL 92

Expert Comment

by:objects
ID: 24015135
>     controlPanel controlPanel = new controlPanel();

typo, that should have been:

    controlPanel controlPanel = null;

0
 

Author Comment

by:mtnrunner2001a
ID: 24015260
Awesome, thanks!  I changed the newUserPanel and everything works perfectly.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question