Jump From Form to Form in an Application

I am going to describe a program that I am trying to build but I don't have any code on this PC to post. If I need to I can make a quick example but I think I can desribe it well enough.

I am making a program that has many different screens that the user can enter data on. When the user clicks the OK or Cancel buttons it will take them to a new screen. For some reason I can't get my program to get rid of the current screen the user is on and open the new one that the program should move to.

Here is how I have it layed out:

I have one main Frame. On this main Frame I have a panel that I call LoaderPanel because it is the panel in which all of the other panels or "subpanels" as I call them will load. Each subpanel is part of the same package but is its own class and is a jpanel with components on it.

When the program opens up I can get a subpanel to load into LoaderPanel just fine. However, when the user is done inputting date on this panel and they click the OK button, I can't get this first subpanel to close and a new panel to open inside of LoaderPanel.

I have tried all sorts of different things with removeAll(), validate(), repaint(), revalidate(), etc. on all different levels. What it seems like to me is when I make calls to my main Frame to tell LoaderPanel to remove its components and then add the new jpanel it just doesn't work.

First off is there a better way for me to do this with Java? Secondly does anyone have any idea how to do what I am trying to accomplish? If this is confusing I can make a quick example and post the code.

Thank You!

[1625 byte] By [Bleara] at [2007-11-27 7:52:51]
# 1
what layout manager you use?i suggest you to use card layout. here is card layout tutorial: http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
j_shadinataa at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

Currently I am just using the Netbeans GUI Builder Matisse which is simply a group layout.

I was thinking of using a cardlayout but when all is said and done I will have around 24 forms that the user could potentially get to. Wouldn't this bog the PC down having 24 forms loaded and moving the order of them around?

Bleara at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 3
validate() is enough then.
j_shadinataa at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I have tried all sorts of different things with removeAll(), validate(),

> repaint(), revalidate(), etc. on all different levels. What it seems like to

> me is when I make calls to my main Frame to tell LoaderPanel to

> remove its components and then add the new jpanel it just doesn't

> work.

i tried with only validate() and it works.

here is my test class:

public class MultiplePanel extends JFrame {

private int i;

private JPanel currPanel;

public JPanel createPanel() {

JPanel p = new JPanel();

p.add(new JLabel(++i + ""));

return p;

}

public MultiplePanel() {

// navi

JPanel btnPnl = new JPanel();

JButton btn = new JButton("Next");

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currPanel = createPanel();

getContentPane().add(currPanel, BorderLayout.CENTER);

validate();

}

});

btnPnl.add(btn);

getContentPane().add(btnPnl, BorderLayout.SOUTH);

}

public static void main(String[] args) {

MultiplePanel j = new MultiplePanel();

j.setDefaultCloseOperation(EXIT_ON_CLOSE);

j.setSize(new Dimension(500, 500));

j.setVisible(true);

}

}

j_shadinataa at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 5

I can get it to work in a case that like as well where there is only one class and it is a Frame and on that frame you load other panels.

What I am trying to do though is have a Frame that has a panel on it that doesn' t ever go away. And on this panel is where the other classes (panels) load. So for my example at least 3 classes would be needed. The main Frame class with the LoaderPanel on it, a class (panel) that gets loaded when the application starts, and a third class (panel) that gets loaded in place of the second class when a button on second class gets pressed.

Bleara at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 6

I've decided to add some code for an example. This code is for the three classes I have described but I didn't put any code into the button yet that would close the current panel and load a new one in its place. Please let me know if you need more clarification.

Code for MainFrame:

package MultiLoaderView;

import javax.swing.*;

import java.awt.*;

/**

*

* @author Blear

*/

public class MainFrame extends javax.swing.JFrame {

/** Creates new form MainFrame */

public MainFrame() {

initComponents();

LoadNewPanel(mySecondClass);

}

/** 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.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

LoaderPanel = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

LoaderPanel.setBackground(new java.awt.Color(255, 255, 204));

javax.swing.GroupLayout LoaderPanelLayout = new javax.swing.GroupLayout(LoaderPanel);

LoaderPanel.setLayout(LoaderPanelLayout);

LoaderPanelLayout.setHorizontalGroup(

LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 792, Short.MAX_VALUE)

);

LoaderPanelLayout.setVerticalGroup(

LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 479, Short.MAX_VALUE)

);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(LoaderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(LoaderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(87, Short.MAX_VALUE))

);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);

}// </editor-fold>

public void LoadNewPanel(Component panel2Load)

{

javax.swing.GroupLayout LoaderPanelLayout = new javax.swing.GroupLayout(LoaderPanel);

LoaderPanel.setLayout(LoaderPanelLayout);

LoaderPanelLayout.setHorizontalGroup(

LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 792, Short.MAX_VALUE)

.addGroup(LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(LoaderPanelLayout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(panel2Load, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE)))

);

LoaderPanelLayout.setVerticalGroup(

LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 479, Short.MAX_VALUE)

.addGroup(LoaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(LoaderPanelLayout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(panel2Load, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE)))

);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MainFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JPanel LoaderPanel;

// End of variables declaration

//Declare instance of SecondClass and ThirdClass

SecondClass mySecondClass = new SecondClass();

ThirdClass myThirdClass = new ThirdClass();

}

Code for SecondClass (second panel)

package MultiLoaderView;

/**

*

* @author Blear

*/

public class SecondClass extends javax.swing.JPanel {

/** Creates new form SecondClass */

public SecondClass() {

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.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

NextPanelButton = new javax.swing.JButton();

DescriptionLabel = new javax.swing.JLabel();

setBackground(new java.awt.Color(204, 255, 204));

NextPanelButton.setText("Next Panel");

NextPanelButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

NextPanelButtonActionPerformed(evt);

}

});

DescriptionLabel.setText("Click to close this panel and open the other");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(93, 93, 93)

.addComponent(DescriptionLabel)

.addContainerGap(101, Short.MAX_VALUE))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(NextPanelButton)

.addGap(0, 0, Short.MAX_VALUE)))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(103, 103, 103)

.addComponent(DescriptionLabel)

.addContainerGap(183, Short.MAX_VALUE))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(NextPanelButton)

.addGap(0, 0, Short.MAX_VALUE)))

);

}// </editor-fold>

private void NextPanelButtonActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

// Variables declaration - do not modify

private javax.swing.JLabel DescriptionLabel;

private javax.swing.JButton NextPanelButton;

// End of variables declaration

}

Code for ThirdClass:

package MultiLoaderView;

/**

*

* @author Blear

*/

public class ThirdClass extends javax.swing.JPanel {

/** Creates new form ThirdClass */

public ThirdClass() {

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.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

setBackground(new java.awt.Color(204, 0, 51));

jLabel1.setText("This is the third class/panel in the project");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(99, 99, 99)

.addComponent(jLabel1)

.addContainerGap(105, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(60, 60, 60)

.addComponent(jLabel1)

.addContainerGap(226, Short.MAX_VALUE))

);

}// </editor-fold>

// Variables declaration - do not modify

private javax.swing.JLabel jLabel1;

// End of variables declaration

}

Bleara at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I've decided to add some code for an example. ......

> .......but I

> didn't put any code into the button yet that would

> close the current panel and load a new one in its

> place. ...........

Why not? Isn't that what you need help with? Let's see what how you have already implemented this and what is not working.

petes1234a at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 8

I didn't include any code for that portion because I have tried many different things as I mentioned in my first post and nothing has worked.

I can get MainFrame to remove its components when I make the calls to do some from within MainFrame, but if I call upon MainFrame to do so from within SecondClass as I want to it just doesn't work.

I've tried making a new instance of MainFrame from within SecondClass and making the calls with that. One thing I did notice is that when I call getContentPane().removeAll() it gets rid of LoaderPanel as well. I take it I am either going to have to keep adding LoaderPanel back and loading a panel into that or find a way to just contact LoaderPanel and have LoaderPanel remove and add the components.

I don't have any one way I am trying to do this, I have tried many different ways. I just want to see if anyone knows a way this will work or if this is even possible.

Bleara at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 9

so 'SecondClass' is your navigation panel.

but i don't see it has any reference to your main frame.

post your complete code in action performed.

private void NextPanelButtonActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

j_shadinataa at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 10

I didn't bother putting anything in because nothing worked.

I tried creating an instance of MainFrame using:

MainFrame myMainFrame = new MainFrame();

Then within NextPanelButtonActionPerformed I have tried a few things. I tried accessing the frame directly by using getContentPane().removeAll(); and I have also tried creating a function within MainFrame to remove the components and calling that new function from the MainFrame instance in SecondClass.

One thing that I suspect might be happening is when I create this instance of MainFrame it is calling its constructor which calls initComponents() which essentially sets up the form as you see if when it loads. Is it possible that it is creating multple instances of the components and when I removeAll() it is removing the top copy?

Bleara at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 11

> One thing that I suspect might be happening is when I

> create this instance of MainFrame it is calling its

> constructor which calls initComponents() which

> essentially sets up the form as you see if when it

> loads. Is it possible that it is creating multple

> instances of the components and when I removeAll() it

> is removing the top copy?

Why would you think that? It creates what you tell it to create, nothing more, nothing less.

petes1234a at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...
# 12

An ineligant solution:

interface Fooable

{

void BtnPushed(Object foo); // The use of Object here is bad form

}

import javax.swing.*;

import java.awt.*;

import java.util.EventListener;

class MainFrame extends javax.swing.JFrame implements Fooable

{

public MainFrame() {

initComponents();

LoadNewPanel(mySecondClass);

}

protected void secondClassBtnPushed()

{

mySecondClass.setVisible(false);

LoadNewPanel(myThirdClass);

this.invalidate();

this.repaint();

}

private void initComponents() {

// no change here

}

public void LoadNewPanel(Component panel2Load)

{

// no change here

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MainFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JPanel LoaderPanel;

// End of variables declaration

//Declare instance of SecondClass and ThirdClass

SecondClass mySecondClass = new SecondClass(this); //!!

ThirdClass myThirdClass = new ThirdClass(this); //!!

public void BtnPushed(Object foo) // The use of Object here is bad form

{

if (foo.equals(mySecondClass))

{

mySecondClass.setVisible(false);

LoadNewPanel(myThirdClass);

this.invalidate();

this.repaint();

}

else if (foo.equals(myThirdClass))

{

}

}

}

class SecondClass extends javax.swing.JPanel

{

Fooable mf; // !!

public SecondClass(Fooable mf) {

this.mf = mf; // !!

initComponents();

}

private void initComponents()

{

// no change here

}// </editor-fold>

private void NextPanelButtonActionPerformed(java.awt.event.ActionEvent evt)

{

mf.BtnPushed(this);

}

// Variables declaration - do not modify

private javax.swing.JLabel DescriptionLabel;

private javax.swing.JButton NextPanelButton;

// End of variables declaration

}

class ThirdClass extends javax.swing.JPanel

{

Fooable mf;

public ThirdClass(Fooable mf)

{

this.mf = mf;

initComponents();

}

private void initComponents()

{

// no change here

}// </editor-fold>

// Variables declaration - do not modify

private javax.swing.JLabel jLabel1;

// End of variables declaration

}

petes1234a at 2007-7-12 19:34:04 > top of Java-index,Desktop,Core GUI APIs...