Solved

Using Invisible Panels to Create a GUI

Posted on 2004-10-03
4
342 Views
Last Modified: 2010-03-31
I'm brushing up on my Java by re-writing a small customer DB application. When I did this for the first time in Uni I made to classic OO beginers mistake of turning one class into the entire program (almost!).

This time round I want to completely seperate the interface from the implemtation and core data. I will do this by creating a GUI class which contains the frame, all the screens (for adding, searching etc..) and stores of the text boxes responces will be stored in as static variable which can be easly accessed by any class handling datainput to the core class.

I'm currently working on the GUI, what I've done is created two panels so far - the main screen and add client. There is a button on the main screen called add client - when it is pressed the main screen panel's visible property is set to false and the add client panel is set to visible. Unfortunately all that happens is the frame goes blank although it should display the add client panel. Also the header label does not display on the main menu panel if I also add it to the add client panel.

I've included the code below.

Thanks

Steve



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class MenuSystem extends javax.swing.JFrame {
   
    private javax.swing.JPanel rootMenuPanel, addClientPanel;
    private javax.swing.JButton viewEditCustBut, addCustBut, remCustBut, dispBadBorBut,
            adjRateBut, loadDBBut, saveDBBut, addCustConfirmBut;
   
    private javax.swing.JLabel headerlbl, templbl;
   
    /* Constructor */
    public MenuSystem(){
        initComponants();
    }

    private void initComponants() {
       
       
         /** Create Header Lable **/
        headerlbl = new javax.swing.JLabel();
        headerlbl.setText("Virtual Bank Customer DB");
        headerlbl.setFont(new java.awt.Font("Karate", 1, 18));
        headerlbl.setBorder(new javax.swing.border.EtchedBorder());
       
        /** Create Root Panel **/
        rootMenuPanel = new javax.swing.JPanel();
        rootMenuPanel.setPreferredSize(new java.awt.Dimension(450, 300));
        rootMenuPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        rootMenuPanel.add(headerlbl, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 260, 40));
       
       
        /** Create Root Panel Buttons **/
        viewEditCustBut = new javax.swing.JButton("View and Edit Customer Details");
        dispBadBorBut = new javax.swing.JButton("Display Bad Borrowers");
        adjRateBut = new javax.swing.JButton("Adjust Rates");
        addCustBut = new javax.swing.JButton("Add Customer");
        remCustBut = new javax.swing.JButton("Remove Customer");
        loadDBBut = new javax.swing.JButton("Load Database");
        saveDBBut = new javax.swing.JButton("Save Database");
       
        /** Add Root Panel Buttons **/
        rootMenuPanel.add(viewEditCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 70, 220, 25));
        rootMenuPanel.add(addCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, 140, 20));
        rootMenuPanel.add(remCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 140, 20));
        rootMenuPanel.add(loadDBBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 120, 140, 20));
        rootMenuPanel.add(saveDBBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 160, 140, 20));
        rootMenuPanel.add(dispBadBorBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 230, 160, 20));
        rootMenuPanel.add(adjRateBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 260, 160, 20));
       
       
        /** Create Add Client Panel **/
        addClientPanel = new javax.swing.JPanel();
        addClientPanel.setVisible(false);
        addClientPanel.setPreferredSize(new java.awt.Dimension(450, 300));
        addClientPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());    
        addClientPanel.add(headerlbl, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 260, 40));
       
        /** Create Client Panel Buttons **/
        addCustConfirmBut = new javax.swing.JButton("addCustConfirmBut");
       
       
        /** Add Add Client Panel Buttons **/
        addClientPanel.add(addCustConfirmBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 70, 220, 25));
       
       
       
        /** Add Button Actions **/
        addCustBut.addActionListener(new java.awt.event.ActionListener(){
           public void actionPerformed(java.awt.event.ActionEvent evt) {
                rootMenuPanel.setVisible(false);
                addClientPanel.setVisible(true);
               
            }
        });
       
       
       
        getContentPane().add(rootMenuPanel, java.awt.BorderLayout.CENTER);
       
        pack();
       
    }
   

}











/*
 * run.java
 *
 * Created on 02 October 2004, 14:39
 */

/**
 *
 * @author  Steve
 */
public class Run {
   
    /** Creates a new instance of run */
    public Run() {
    }
   
    public static void main(String[] args) {
        MenuSystem myMenuSystem;
        myMenuSystem = new MenuSystem();
        myMenuSystem.setVisible(true);
    }
}


0
Comment
Question by:SketchySteve
  • 2
4 Comments
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12214233
For the header labels, simply make two and give each a different label. As for your problem with the client panel not showing, make sure you add it to the content pane as it doesn't seem that you do in the above code. If you don't add the panel to a Frame/JFrame.getContentPane(), it can't be displayed. ;)
0
 
LVL 1

Author Comment

by:SketchySteve
ID: 12216200
I've added the client panel to the frame using

getContentPane().add(addClientPanel, java.awt.BorderLayout.CENTER);

but to no avail. Here is my modified code, why's dosnt the header label appear on either panel either?

Cheers

Steve


/*
 * run.java
 *
 * Created on 02 October 2004, 14:39
 */

/**
 *
 * @author  Steve
 */
public class Run {
   
    /** Creates a new instance of run */
    public Run() {
    }
   
    public static void main(String[] args) {
        MenuSystem myMenuSystem;
        myMenuSystem = new MenuSystem();
        myMenuSystem.setVisible(true);
    }
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class MenuSystem extends javax.swing.JFrame {
   
    private javax.swing.JPanel rootMenuPanel, addClientPanel;
    private javax.swing.JButton viewEditCustBut, addCustBut, remCustBut, dispBadBorBut,
            adjRateBut, loadDBBut, saveDBBut, addCustConfirmBut;
   
    private javax.swing.JLabel headerlbl, templbl;
   
    /* Constructor */
    public MenuSystem(){
        initComponants();
    }

    private void initComponants() {
       
       
         /** Create Header Lable **/
        headerlbl = new javax.swing.JLabel();
        headerlbl.setText("Virtual Bank Customer DB");
        headerlbl.setFont(new java.awt.Font("Karate", 1, 18));
        headerlbl.setBorder(new javax.swing.border.EtchedBorder());
       
        /** Create Root Panel **/
        rootMenuPanel = new javax.swing.JPanel();
        rootMenuPanel.setPreferredSize(new java.awt.Dimension(450, 300));
        rootMenuPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        rootMenuPanel.add(headerlbl, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 260, 40));
       
       
        /** Create Root Panel Buttons **/
        viewEditCustBut = new javax.swing.JButton("View and Edit Customer Details");
        dispBadBorBut = new javax.swing.JButton("Display Bad Borrowers");
        adjRateBut = new javax.swing.JButton("Adjust Rates");
        addCustBut = new javax.swing.JButton("Add Customer");
        remCustBut = new javax.swing.JButton("Remove Customer");
        loadDBBut = new javax.swing.JButton("Load Database");
        saveDBBut = new javax.swing.JButton("Save Database");
       
        /** Add Root Panel Buttons **/
        rootMenuPanel.add(viewEditCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 70, 220, 25));
        rootMenuPanel.add(addCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, 140, 20));
        rootMenuPanel.add(remCustBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 140, 20));
        rootMenuPanel.add(loadDBBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 120, 140, 20));
        rootMenuPanel.add(saveDBBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 160, 140, 20));
        rootMenuPanel.add(dispBadBorBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 230, 160, 20));
        rootMenuPanel.add(adjRateBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 260, 160, 20));
       
       
        /** Create Add Client Panel **/
        addClientPanel = new javax.swing.JPanel();
        addClientPanel.setVisible(false);
        addClientPanel.setPreferredSize(new java.awt.Dimension(450, 300));
        addClientPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());    
        addClientPanel.add(headerlbl, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 260, 40));
       
        /** Create Client Panel Buttons **/
        addCustConfirmBut = new javax.swing.JButton("Confirm");
       
       
        /** Add Add Client Panel Buttons **/
        addClientPanel.add(addCustConfirmBut, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 70, 220, 25));
       
       
       
        /** Add Button Actions **/
        addCustBut.addActionListener(new java.awt.event.ActionListener(){
           public void actionPerformed(java.awt.event.ActionEvent evt) {
                rootMenuPanel.setVisible(false);
                addClientPanel.setVisible(true);
               
            }
        });
       
        rootMenuPanel.setVisible(true);
        addClientPanel.setVisible(false);
       
        getContentPane().add(addClientPanel, java.awt.BorderLayout.CENTER);
        getContentPane().add(rootMenuPanel, java.awt.BorderLayout.CENTER);
       
       
        pack();
       
    }
   

}
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 125 total points
ID: 12216333
You are adding two objects into the center, the layout manager is Borderlayout.  You are overwriting it, or put in other words your are confusing the layout manager (I would be confused).  I would say to simply set one panel in the center.  Then register an instance of a CardLayout layout manager for that panel.  Then when the action occurs that which means you want to view the other panel, notify the cardlayout to switch panels.  That sounds like the action that you are desiring.  You may want to take a look at the JavaDocs on it and possibly the tutorial just to refresh yourself.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12217416
thanks!
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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
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 …

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

11 Experts available now in Live!

Get 1:1 Help Now