Solved

Using Invisible Panels to Create a GUI

Posted on 2004-10-03
4
363 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
[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
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

630 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