Layout Managers

Nicholas Duchon: Mar 31, 2017

Key Concepts:


Example:

Select among these examples by using the JTabbedPane tabs at the top.

FlowLayout - Mac:


BorderLayout - Mac:


GridLayout (0,3) - PC:


GridLayout (3,0) - PC:


null layout - PC:


JSplitPane layout - PC:



Code:

// File: DiscoveryOfLayoutManagers.java
// Date: Jul 09, 2013
// Author: Nicholas Duchon
// Purpose: demonstrate how layout managers work
// Version: 1.0
// History:

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.0";
   
    JButton jb_1 = new JButton ("One");
    JButton jb_2 = new JButton ("Two");
   
    public DiscoveryOfLayoutManagers () {
   
} // end no-parameter constructor
   
    public void init () {
        JTabbedPane jtb = new JTabbedPane ();
       
        jtb.addTab ("Flow Layout",        showFlow   ());
        jtb.addTab ("Border Layout",      showBorder ());
        jtb.addTab ("Grid Layout (0,3)",  showGridA  ());
        jtb.addTab ("Grid Layout (3, 0)", showGridB  ());
        jtb.addTab ("null",                  showNull   ());
        jtb.addTab ("Split",              showSplit  ());

        add (jtb);

        validate ();
   
} // end init
   
    JPanel showFlow () {
        JPanel jp = new JPanel ();
        jp.add (new JButton ("one"));
        jp.add (new JButton ("two"));
        jp.add (new JButton ("three"));
        jp.add (new JButton ("four"));
        jp.add (new JButton ("five"));
        jp.add (new JButton ("six"));
        jp.add (new JButton ("seven"));
        jp.add (new JButton ("eight"));
        jp.add (new JButton ("nine"));
        jp.add (new JButton ("ten"));
        jp.add (new JButton ("eleven"));
        jp.add (new JButton ("twelve"));
        jp.add (new JButton ("thirteen"));
        jp.add (new JButton ("fourteen"));
        jp.add (new JButton ("fifteen"));
        jp.add (new JButton ("sixteen"));
        jp.add (new JButton ("seventeen"));
        jp.add (new JButton ("eighteen"));
        jp.add (new JButton ("nineteen"));
        jp.add (new JButton ("twenty"));
        jp.add (new JButton ("twenty-one"));
        jp.add (new JButton ("twenty-two"));
        jp.add (new JButton ("twenty-three"));
        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 () {
        JPanel jp = new JPanel ();
        jp.setLayout (new GridLayout (0, 3));
        jp.add (new JButton ("one"));
        jp.add (new JButton ("two"));
        jp.add (new JButton ("three"));
        jp.add (new JButton ("four"));
        jp.add (new JButton ("five"));
        jp.add (new JButton ("six"));
        jp.add (new JButton ("seven"));
        jp.add (new JButton ("eight"));
        jp.add (new JButton ("nine"));
        jp.add (new JButton ("ten"));
        jp.add (new JButton ("eleven"));
        jp.add (new JButton ("twelve"));
        jp.add (new JButton ("thirteen"));
        jp.add (new JButton ("fourteen"));
        jp.add (new JButton ("fifteen"));
        jp.add (new JButton ("sixteen"));
        jp.add (new JButton ("seventeen"));
        jp.add (new JButton ("eighteen"));
        jp.add (new JButton ("nineteen"));
        jp.add (new JButton ("twenty"));
        jp.add (new JButton ("twenty-one"));
        jp.add (new JButton ("twenty-two"));
        jp.add (new JButton ("twenty-three"));
        return jp;
   
} // end showGridA
   
    JPanel showGridB () {
        JPanel jp = new JPanel ();
        jp.setLayout (new GridLayout (3, 0));
        jp.add (new JButton ("one"));
        jp.add (new JButton ("two"));
        jp.add (new JButton ("three"));
        jp.add (new JButton ("four"));
        jp.add (new JButton ("five"));
        jp.add (new JButton ("six"));
        jp.add (new JButton ("seven"));
        jp.add (new JButton ("eight"));
        jp.add (new JButton ("nine"));
        jp.add (new JButton ("ten"));
        jp.add (new JButton ("eleven"));
        jp.add (new JButton ("twelve"));
        jp.add (new JButton ("thirteen"));
        jp.add (new JButton ("fourteen"));
        jp.add (new JButton ("fifteen"));
        jp.add (new JButton ("sixteen"));
        jp.add (new JButton ("seventeen"));
        jp.add (new JButton ("eighteen"));
        jp.add (new JButton ("nineteen"));
        jp.add (new JButton ("twenty"));
        jp.add (new JButton ("twenty-one"));
        jp.add (new JButton ("twenty-two"));
        jp.add (new JButton ("twenty-three"));
        return jp;
   
} // end showGridB
   
    JPanel showNull () {
        JButton jb;
        JPanel jp = new JPanel ();
        jp.setLayout (null);
        int wid = 65;
        jp.add (jb = new JButton ("one"));
        jb.setBounds (10, 10, wid, 20);
        jp.add (jb = new JButton ("two"));
        jb.setBounds (100, 10, wid, 20);
        jp.add (jb = new JButton ("three"));
        jb.setBounds (10, 100, wid, 20);
        jp.add (jb = new JButton ("four"));
        jb.setBounds (200, 10, wid, 20);
        jp.add (jb = new JButton ("five"));
        jb.setBounds (400, 200, wid, 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


More SplitPanes

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

(end)


ND.