In this tutorial

You will learn how to...
  • How to add JSeparators to a GUI.

JSeparator

The JSeparator widget is a simple little thing that draws a line to separate widgets in a Layout. It can be instantiated with a single argument to establish which way the line is to be drawn. To save time, I've quickly introduced these to the previous example. Let's look at snippets of code and the outcome. This first code & picture is from the previous example without the Separators.
83
84
85
86
87
88
89
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(storyArea);
        bottomPanel.add(Box.createRigidArea(new Dimension(20,0)));
        bottomPanel.add(sexPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(20,0)));
        bottomPanel.add(cbPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
Separator Picture

In this version we put in separators between the JPanels at Lines 86 & 90.
83
84
85
86
87
88
89
90
91
92
93
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(storyArea);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(new JSeparator(SwingConstants.VERTICAL));
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(sexPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(new JSeparator(SwingConstants.VERTICAL));
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(cbPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
Second Separator Picture

As you can see, all the JSeparator does is to make a line that suits the look and feel of the application to split up the GUI into sections.
This is especially useful for the JMenu which we will look at next.


Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict