Re: Multiple Panels
CHAFIK Wassime wrote:
Mark Space wrote:
michael.miceli88@gmail.com wrote:
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>
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);
}
});
}
}
--
Knute Johnson
email s/nospam/linux/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem