Solved

Using Invisible Panels to Create a GUI

Posted on 2004-10-03
4
330 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks!
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

Title # Comments Views Activity
changePi Challenge 15 75
Systems talking to each other 5 107
Python Assistance 7 30
solarwind tftp server 2 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 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

10 Experts available now in Live!

Get 1:1 Help Now