Solved

How to switch between different JPanels using CardLayout and references

Posted on 2009-03-29
6
2,139 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
  • 3
  • 3
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 73
Recommendation vb6 to vb.net or others 14 112
Support for Notepad++ (including downloading & installing a plugin) 5 69
javap not working 8 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now