Multiple Panels

M

michael.miceli88

Hi,
I am programming a GUI for a Java program I wrote, and have a
question. I have one Frame with some buttons and labels on them, but
when I am done I want the user to be able to hit next and get to more
information. How do programs do this in general. I could create a
new panel and hide the other one, but is this the best solution?

Thanks
Michael
 
K

Knute Johnson

Hi,
I am programming a GUI for a Java program I wrote, and have a
question. I have one Frame with some buttons and labels on them, but
when I am done I want the user to be able to hit next and get to more
information. How do programs do this in general. I could create a
new panel and hide the other one, but is this the best solution?

Thanks
Michael

When you press the "Next" button, remove the panel with the buttons and
labels and add the panel with the other information. Remember to call
validate() on the Frame after you add your new panel.

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

public class test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel p2 = new JPanel();
p2.add(new JLabel("Info Panel"));

final JPanel p1 = new JPanel(new BorderLayout());
p1.add(new JLabel("Label"),BorderLayout.NORTH);
JButton b = new JButton("Next");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
f.remove(p1);
f.add(p2,BorderLayout.CENTER);
f.validate();
}
});
p1.add(b,BorderLayout.SOUTH);

f.add(p1,BorderLayout.CENTER);

f.pack();
f.setVisible(true);
}
});
}
}
 
M

Mark Space

Hi,
I am programming a GUI for a Java program I wrote, and have a
question. I have one Frame with some buttons and labels on them, but
when I am done I want the user to be able to hit next and get to more
information. How do programs do this in general. I could create a
new panel and hide the other one, but is this the best solution?

Thanks
Michael

Besides Knute's excellent demonstration, you should also look at the
CardLayout, which lets you switch panels some what automagically.

<http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html>
 
M

michael.miceli88

Hi,
I am programming a GUI for a Java program I wrote, and have a
question. I have one Frame with some buttons and labels on them, but
when I am done I want the user to be able to hit next and get to more
information. How do programs do this in general. I could create a
new panel and hide the other one, but is this the best solution?
Thanks
Michael

When you press the "Next" button, remove the panel with the buttons and
labels and add the panel with the other information. Remember to call
validate() on the Frame after you add your new panel.

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

public class test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel p2 = new JPanel();
p2.add(new JLabel("Info Panel"));

final JPanel p1 = new JPanel(new BorderLayout());
p1.add(new JLabel("Label"),BorderLayout.NORTH);
JButton b = new JButton("Next");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
f.remove(p1);
f.add(p2,BorderLayout.CENTER);
f.validate();
}
});
p1.add(b,BorderLayout.SOUTH);

f.add(p1,BorderLayout.CENTER);

f.pack();
f.setVisible(true);
}
});
}

}

Thanks a lot :-D
 
K

Knute Johnson

CHAFIK said:
hi
a good thing about CardLayout is you can implement a "back" button and
you'll have all the inputs that the user made

CardLayout is really slick but there are some interesting issues that
can come up. Note in the rewritten example below, I set the layout
manager on the JFrame to a CardLayout. JFrame.setLayout() is overridden
in version 1.5+ to forward this call to the content pane. But in the
CardLayout.next() and .previous calls, I have to specify the content
pane of the JFrame as the container not just the JFrame. There are some
significant advantages to using CardLayout. When the "cards" are added
and the container packed, the container will be sized to fit the largest
of the "cards". Random access to the cards is very simple too with the
show() and the constraints object. Check out the docs for description
of the API but look around the net for examples of how to use CardLayout.

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

public class test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final CardLayout cl = new CardLayout();
f.setLayout(cl);

final JPanel p1 = new JPanel(new BorderLayout());
final JPanel p2 = new JPanel(new BorderLayout());

p1.add(new JLabel(
"Labels and Buttons"),BorderLayout.NORTH);
JButton b = new JButton("Next");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.next(f.getContentPane());
}
});
p1.add(b,BorderLayout.SOUTH);
f.add(p1,"First");

p2.add(new JLabel("Info Panel"),BorderLayout.NORTH);
b = new JButton("Previous");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// cl.previous(f.getContentPane());
cl.show(f.getContentPane(),"First");
}
});
p2.add(b,BorderLayout.SOUTH);
f.add(p2,"Second");

f.pack();
f.setVisible(true);
}
});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top