Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.
Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg
by cochand » Wed Aug 17, 2005 5:32 pm
Please refer to the SWT post of Wed Jul 06, 2005 1:35 pm entitled Nested SWT Layouts
I was able to build a great SWT app that had a SWT Tree on the left 1/3 of the app window and a stack of composites on the right 2/3 of the screen. Clicking on a node of the tree displayed a different composite on the right side of the screen.
Now I need to recreate this in Swing and am looking for advice.
Should I use a Swing FormLayout and stick a JTree in the left 1/3 and a Swing CardLayout in the right 2/3? Can I create the same type of navigation of clicking on a node of the JTree, and dynamically displaying a separate "card" on the right side? Should those cards contain JPanels?
Thanks
-
cochand
-
- Posts: 17
- Joined: Wed Jul 06, 2005 1:20 pm
by Eric Clayberg » Thu Aug 18, 2005 6:07 am
cochand wrote:I was able to build a great SWT app that had a SWT Tree on the left 1/3 of the app window and a stack of composites on the right 2/3 of the screen. Clicking on a node of the tree displayed a different composite on the right side of the screen. Now I need to recreate this in Swing and am looking for advice. Should I use a Swing FormLayout and stick a JTree in the left 1/3 and a Swing CardLayout in the right 2/3? Can I create the same type of navigation of clicking on a node of the JTree, and dynamically displaying a separate "card" on the right side? Should those cards contain JPanels?
You can use a JTree and a JPanel with a CardLayout. The CardLayout would itself contain multiple JPanels which you could switch between based on selections in the tree using the CardLayout.show() method.
Here's a simple example:
- Code: Select all
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SwingFrame extends JFrame {
public static void main(String args[]) {
try {
SwingFrame frame = new SwingFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public SwingFrame() {
super();
getContentPane().setLayout(new BorderLayout());
setBounds(100, 100, 500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final CardLayout card = new CardLayout();
panel.setLayout(card);
getContentPane().add(panel);
final JPanel first = new JPanel();
first.setName("A");
panel.add(first, first.getName());
final JButton button = new JButton();
button.setText("Panel A");
first.add(button);
final JPanel second = new JPanel();
second.setName("B");
panel.add(second, second.getName());
final JTextField textField = new JTextField();
textField.setText("Panel B");
second.add(textField);
final JList list = new JList(new String[] {"A", "B"});
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
card.show(panel, (String)list.getSelectedValues()[0]);
}
});
list.setPreferredSize(new Dimension(50, 0));
getContentPane().add(list, BorderLayout.WEST);
}
}
-
Eric Clayberg
- Moderator
-
- Posts: 4503
- Joined: Tue Sep 30, 2003 6:39 am
- Location: Boston, MA USA
-
Return to Swing Designer
Who is online
Users browsing this forum: No registered users and 1 guest