Layout Managers
|
Key Concepts:
|
Select among these examples by using the JTabbedPane tabs at the
top.
// File: DiscoveryOfLayoutManagers.java
// Date: Jul 09, 2013
// Author: Nicholas Duchon
// Purpose: demonstrate how layout managers work
// Version: 1.1
// History:
// Sep 11, 2018 - used String [] list
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JSplitPane;
// import java.awt.CardLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
public class DiscoveryOfLayoutManagers extends JApplet {
static final long serialVersionUID = 12334L;
static String version = "Version 1.1";
String [] list = {
"one", "two", "three", "four",
"five", "six", "seven", "eight",
"nine", "ten", "eleven",
"twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen",
"eighteen", "nineteen", "twenty",
"twenty-one", "twenty-two",
"twenty-three"
}; // end list string
public void init () {
JTabbedPane jtb = new JTabbedPane ();
jtb.addTab ("Flow", showFlow ());
jtb.addTab ("Border", showBorder ());
jtb.addTab ("Grid (0,3)", showGridA ());
jtb.addTab ("Grid (3,0)", showGridB ());
jtb.addTab ("null", showNull ());
jtb.addTab ("Split", showSplit ());
add (jtb);
validate ();
} // end init
JPanel showFlow () {
JPanel jp = new JPanel ();
for (String s: list) jp.add (new JButton (s));
return jp;
} // end showFlow
JPanel showBorder () {
JPanel jp = new JPanel ();
jp.setLayout (new BorderLayout ());
jp.add (new JButton ("North") , BorderLayout.NORTH);
jp.add (new JButton ("West") , BorderLayout.WEST);
jp.add (new JButton ("Center"), BorderLayout.CENTER);
jp.add (new JButton ("East") , BorderLayout.EAST);
jp.add (new JButton ("South") , BorderLayout.SOUTH);
return jp;
} // end showBorder
JPanel showGridA () { // 0,3
JPanel jp = new JPanel ();
jp.setLayout (new GridLayout (0, 3));
for (String s: list) jp.add (new JButton (s));
return jp;
} // end showGridA
JPanel showGridB () { // 3,0
JPanel jp = new JPanel ();
jp.setLayout (new GridLayout (3, 0));
for (String s: list) jp.add (new JButton (s));
return jp;
} // end showGridB
JPanel showNull () {
JButton jb;
JPanel jp = new JPanel ();
jp.setLayout (null);
jp.add (jb = new JButton ("one"));
jb.setBounds (10, 10, 50, 20);
jp.add (jb = new JButton ("two"));
jb.setBounds (100, 10, 50, 20);
jp.add (jb = new JButton ("three"));
jb.setBounds (10, 100, 50, 20);
jp.add (jb = new JButton ("four"));
jb.setBounds (200, 10, 50, 20);
jp.add (jb = new JButton ("five"));
jb.setBounds (400, 200, 50, 20);
return jp;
} // end showGridB
JSplitPane showSplit () {
JSplitPane jp = new JSplitPane (
JSplitPane.HORIZONTAL_SPLIT,
showGridA(),
showBorder()
);
return jp;
} // end showSplit
public static void main (String args []) {
DiscoveryOfLayoutManagers dom = new DiscoveryOfLayoutManagers ();
JFrame jf = new JFrame ("Discovery of Layout Managers " + version);
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
jf.setSize (800, 700);
jf.setLocationRelativeTo (null);
jf.add (dom, BorderLayout.CENTER);
jf.setVisible (true);
dom.init();
dom.start();
} // end main
} // end class
Here is some code demonstrating some of the features of JTree, DMTN (DefaultMutableTreeNode), and JSplitPane - this is not perfect since the desired splitting doesn't seem to work until the JFrame is resized. I am still working on fixing that problem.
// File: JTreeExample2
// Date: Jul 19, 2013
// Author: Nicholas Duchon
// Purpose: a quick demonstration of populating and presenting a
JTree
// createDataTree creates a tree structure directly
as a JTree using DefaultMutableTreeNode's as elements of the
tree
// the DMTN references Element's, which are really
any Object you wish to define.
// Display - this code shows how to display the
results of various traversals also.
// Also added JSplitPane's to present traversals in
GUI's
// Notes -
// DMTN only has one set of children at each node,
so if your project is more complex, you will need to fix this
somehow.
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Enumeration; // used for display,
note that this is normally generic, but DMTN enumerations are
NOT!
public
class JTreeExample2 {
int paneCount = 5;
JSplitPane jsppA;
JSplitPane jsppB;
JSplitPane jsppC;
JSplitPane jsppD;
JFrame jf;
public static void main (String args []) {
JTreeExample2 j
= new JTreeExample2 ();
} // end main
@SuppressWarnings ("unchecked")
public JTreeExample2 () {
DefaultMutableTreeNode root = createNodes ();
JTree tree =
new JTree(root);
JScrollPane
treeView = new JScrollPane(tree);
JTextArea jtaA
= new JTextArea ();
JScrollPane
jspA = new JScrollPane (jtaA);
JTextArea jtaB
= new JTextArea ();
JScrollPane
jspB = new JScrollPane (jtaB);
JTextArea jtaC
= new JTextArea ();
JScrollPane
jspC = new JScrollPane (jtaC);
JTextArea jtaD
= new JTextArea ();
JScrollPane
jspD = new JScrollPane (jtaD);
jsppA = new
JSplitPane (JSplitPane.HORIZONTAL_SPLIT, treeView, jspA);
jsppB = new
JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppA ,
jspB);
jsppC = new
JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppB ,
jspC);
jsppD = new
JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppC ,
jspD);
jf = new JFrame
("JTree example - ND");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add (jsppD,
BorderLayout.CENTER);
jtaA.setText
("--- Post Order:");
for
(Enumeration <DefaultMutableTreeNode> e =
root.postorderEnumeration(); e.hasMoreElements();) {
DefaultMutableTreeNode d = e.nextElement();
jtaA.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s
- %s\n", "", d.toString()));
} // end
printing tree
jtaB.setText
("--- breadthFirstEnumeration Order:");
for
(Enumeration <DefaultMutableTreeNode> e =
root.breadthFirstEnumeration(); e.hasMoreElements();) {
DefaultMutableTreeNode d = e.nextElement();
jtaB.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s
- %s\n", "", d.toString()));
} // end
printing tree
jtaC.setText
("--- depthFirstEnumeration Order:");
for
(Enumeration <DefaultMutableTreeNode> e =
root.depthFirstEnumeration(); e.hasMoreElements();) {
DefaultMutableTreeNode d = e.nextElement();
jtaC.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s
- %s\n", "", d.toString()));
} // end
printing tree
jtaD.setText
("--- preorderEnumeration Order:");
for
(Enumeration <DefaultMutableTreeNode> e =
root.preorderEnumeration(); e.hasMoreElements();) {
DefaultMutableTreeNode d = e.nextElement();
jtaD.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s
- %s\n", "", d.toString()));
} // end
printing tree
jf.pack ();
jf.setLocationRelativeTo (null);
jf.setVisible
(true);
jf.addComponentListener (
new ComponentAdapter () {
public void componentResized (ComponentEvent e) {resizeMe();}
public void componentShown (ComponentEvent e)
{resizeMe();}
});
} // end constructor
void resizeMe () {
int n =
paneCount;
jsppD.setDividerLocation (1.0 - 1.0 / n--);
jsppC.setDividerLocation (1.0 - 1.0 / n--);
jsppB.setDividerLocation (1.0 - 1.0 / n--);
jsppA.setDividerLocation (1.0 - 1.0 / n--);
} // end resizeMe method
DefaultMutableTreeNode createNodes () {
DefaultMutableTreeNode t, u, v, w, x, y, z;
z = new
DefaultMutableTreeNode (new Element
("zero" ));
z.add
(t = new
DefaultMutableTreeNode (new Element ("one
A" )));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoA A"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoA B"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoA C"
)));
u.add (w = new
DefaultMutableTreeNode (new Element ("threeAC A" )));
u.add (w = new
DefaultMutableTreeNode (new Element ("threeAC B" )));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoA D"
)));
z.add (t
= new DefaultMutableTreeNode (new Element
("one B" )));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoB A"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoB B"
)));
u.add (w = new
DefaultMutableTreeNode (new Element ("threeBB A" )));
u.add (w = new
DefaultMutableTreeNode (new Element ("threeBB B" )));
w.add (x = new
DefaultMutableTreeNode (new Element ("threeBBB A")));
w.add (x = new
DefaultMutableTreeNode (new Element ("threeBBB B")));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoB C"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoB D"
)));
z.add (t
= new DefaultMutableTreeNode (new Element ("one
C" )));
t.add (u = new DefaultMutableTreeNode
(new Element ("twoC A" )));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoC B"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoC C"
)));
t.add (u = new DefaultMutableTreeNode
(new Element ("twoC D" )));
z.add (t
= new DefaultMutableTreeNode (new Element ("one
D" )));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoD A"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoD B"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoD C"
)));
t.add (u = new
DefaultMutableTreeNode (new Element ("twoD D"
)));
return z;
} // end createDataTree
}
// end class JTreeExample
class
Element {
String data;
public Element (String s) {data = s;}
public String toString () {
return data;
}
}
// end Node
// File: MunchND.java
// Date: Jul 13, 2018
// Author: Nicholas Duchon
// Purpose:
// show how to handle JPanels with FlowLayout
// or lots of stuff with JSplitPane's
// use setting minimum member panel sizes
// Reference:
// https://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Dimension;
public class MunchND {
public static final long serialVersionUID = 123; // ND: junk
public MunchND () {
FrameND f = new FrameND ("MunchND v 0.1");
f.getContentPane().setBackground (Color.yellow);
JPanel jpA = new JPanel ();
JPanel jpB = new JPanel ();
JPanel jpC = new JPanel ();
jpA.add (new JButton ("asdf"));
jpA.add (new JButton ("LKJFG"));
jpA.add (new JButton ("oiutrei"));
jpA.add (new JButton ("wervxc"));
jpA.add (new JButton ("9238247"));
jpA.add (new JButton ("213245678"));
jpB.add (new JButton ("asdf"));
jpB.add (new JButton ("LKJFG"));
jpB.add (new JButton ("oiutrei"));
jpB.add (new JButton ("wervxc"));
jpB.add (new JButton ("9238247"));
jpB.add (new JButton ("213245678"));
jpC.add (new JButton ("asdf"));
jpC.add (new JButton ("LKJFG"));
jpC.add (new JButton ("oiutrei"));
jpC.add (new JButton ("wervxc"));
jpC.add (new JButton ("9238247"));
jpC.add (new JButton ("213245678"));
// jpA.setLayout (new GridLayout (0, 2));
// jpB.setLayout (new GridLayout (0, 2));
// jpC.setLayout (new GridLayout (0, 2));
JSplitPane jspA = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jpA, jpB);
JSplitPane jspB = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jspA, jpC);
Dimension minimumSize = new Dimension(0, 0);
jpA.setMinimumSize(minimumSize);
jpB.setMinimumSize(minimumSize);
jpC.setMinimumSize(minimumSize);
jspB.setMinimumSize(minimumSize);
jspA.setBackground (Color.pink);
jspB.setBackground (Color.green);
f.add (jspB);
f.pack();
} // end no-parameter constructor
public static void main (String [] args) {
MunchND m = new MunchND ();
} // end main
} // end class MunchND
class FrameND extends javax.swing.JFrame {
public static final long serialVersionUID = 892374; // ND: junk
static final int WIDTH = 400, HEIGHT = 200;
static final int DX = 20, DY = 20;
static int windowCount = 0;
static FrameND prev = null;
public FrameND () {
windowCount ++;
setSize (WIDTH, HEIGHT);
if (windowCount == 1) {
setLocationRelativeTo (null);
}
else {
java.awt.Point p = prev.getLocation();
setLocation (p.x + DX, p.y + DY);
} // end creating cascading frames
prev = this;
validate ();
setTitle ("Frame Number: " + windowCount);
setVisible (true);
// setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
addWindowListener (
new java.awt.event.WindowAdapter () {
public void windowClosing (java.awt.event.WindowEvent e) {
windowCount --;
if (windowCount == 0) System.exit (0);
} // end closing event handler method
} // end new inner adapter
); // end adding anonymous inner class listener
} // end no-parameter constructor
public FrameND (String s) {
this ();
setTitle (s);
} // String constructor
public FrameND (String s, int w, int h) {
this (s);
setSize (w, h);
} // end String-int-int constructor
} // end class FrameND
Date: July 13, 2018