Solved

Using Invisible Panels to Create a GUI

Posted on 2004-10-03
4
351 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 37
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 40
Setup GlassFish 4 34
batch vs regular insert in spring DAO 2 20
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

740 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