Solved

How to switch between different JPanels using CardLayout and references

Posted on 2009-03-29
6
2,144 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

813 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

16 Experts available now in Live!

Get 1:1 Help Now