Jump to content

Java Tutorial/Intermediate GUIs/Placing components

From Wikiversity

Placing components

[edit | edit source]

...

A utility class for placing and grouping components

[edit | edit source]

The following utility class offers some functions for placing and grouping compnents. It supports the GridBagLayout with GridBagConstraints, a use of BorderLayout, adding a component in a JScrollPane and grouping components with one or several JSplitPane components. Of course there are many other useful methods one might want to have and you should expand the class for your own programs.

public final class UIUtil
{
    public final static void addWithGridBagConstraints (Container parent, JComponent child, int gridx, int gridy, int gridwidth, int gridheight, int fill, double weightx, double weighty)
    {
        GridBagConstraints c = new GridBagConstraints ();
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.fill = fill;
        c.weightx = weightx;
        c.weighty = weighty;
        c.insets = new Insets (5,5,5,5);
        parent.add (child, c);
    }    
    
    public final static void addCenter (Container parent, JComponent child)
    {
        BorderLayout layout = new BorderLayout ();
        parent.setLayout (layout);
        parent.add (child, BorderLayout.CENTER);
    }

    public final static JScrollPane addWithScrollPane (Container parent, JComponent child)
    {
        BorderLayout layout = new BorderLayout ();
        parent.setLayout (layout);
        JScrollPane scrollPane = new JScrollPane (child);
        scrollPane.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        parent.add (scrollPane, BorderLayout.CENTER);
        return scrollPane;
    }

    public final static JSplitPane groupWithVerticalSplitPane (JComponent c1, int minHeight1, JComponent c2, int minHeight2)
    {
        JSplitPane splitPane = new JSplitPane (JSplitPane.VERTICAL_SPLIT, c1, c2);
        splitPane.setOneTouchExpandable (true);
        c1.setMinimumSize (new Dimension (0, minHeight1));
        c2.setMinimumSize (new Dimension (0, minHeight2));
        return splitPane;
    }

    public final static JSplitPane[] groupWithVerticalSplitPanes (JComponent c1, int minHeight1, JComponent c2, int minHeight2, JComponent c3, int minHeight3)
    {
        JSplitPane[] splitPane = new JSplitPane[2];
        splitPane[1] = new JSplitPane (JSplitPane.VERTICAL_SPLIT, c1, c2);
        c1.setBorder (null);
        c2.setBorder (null);
        splitPane[0] = new JSplitPane (JSplitPane.VERTICAL_SPLIT, splitPane[1], c3);
        splitPane[1].setOneTouchExpandable (true);
        splitPane[0].setOneTouchExpandable (true);
        c1.setMinimumSize (new Dimension (0, minHeight1));
        c2.setMinimumSize (new Dimension (0, minHeight2));
        c3.setMinimumSize (new Dimension (0, minHeight3));
        return splitPane;
    }
    
    public final static JSplitPane groupWithHorizontalSplitPane (JComponent c1, int minWidth1, JComponent c2, int minWidth2)
    {
        JSplitPane splitPane = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, c1, c2);
        c1.setBorder (null);
        c2.setBorder (null);
        splitPane.setOneTouchExpandable (true);
        c1.setMinimumSize (new Dimension (minWidth1, 0));
        c2.setMinimumSize (new Dimension (minWidth2, 0));
        return splitPane;
    }
    
    public final static JSplitPane[] groupWithHorizontalSplitPanes (JComponent c1, int minWidth1, JComponent c2, int minWidth2, JComponent c3, int minWidth3)
    {
        JSplitPane[] splitPane = new JSplitPane[2];
        splitPane[1] = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, c1, c2);
        c1.setBorder (null);
        c2.setBorder (null);
        splitPane[0] = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, splitPane[1], c3);
        splitPane[1].setOneTouchExpandable (true);
        splitPane[0].setOneTouchExpandable (true);
        c1.setMinimumSize (new Dimension (minWidth1, 0));
        c2.setMinimumSize (new Dimension (minWidth2, 0));
        c3.setMinimumSize (new Dimension (minWidth3, 0));
        return splitPane;
    }
}
[edit | edit source]