Solved

How to switch between different JPanels using CardLayout and references

Posted on 2009-03-29
6
2,137 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
Comment Utility
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
Comment Utility
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
Comment Utility
can you post your code

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:mtnrunner2001a
Comment Utility
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
Comment Utility
>     controlPanel controlPanel = new controlPanel();

typo, that should have been:

    controlPanel controlPanel = null;

0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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