Solved

How to switch between different JPanels using CardLayout and references

Posted on 2009-03-29
6
2,150 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

730 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