banner

JTabbedPanes, JScrollPanes and JSplitPanes

Optional Reading

We will be using the Creating a GUI with JFC/Swing tutorial from Sun as a Reference source. You are not required to read the entire tutorial, I suggest you skim through it though to use a a supplement to what I give you here.

JTabbedPanes

JTabbedPanes are a convenience container that provides you with a row of tabs that show different panels when selected. To use a JTabbedPane, you create an empty one with the constructor and then add components to the tabbed pane, giving each a label.

JTabbedPane jtp = new JTabbedPane();
JPanel panel = new JPanel()
jtp.addTab(panel, "Tab Label")

There are a few different options for using JTabbedPanes, the most notable is that in the Java Look and Feel you can have multiple rows of tabs or a single row of "scrollable" tabs. You can also attach a listener to the selection of the tabs and execute code when somebody changes the tabs.

Download and open the Netbeans project jtabbedpane. When you first start the application, the following window appears.

JTabbedPane 1

If you click on the "This is Page 2" tab, and then press the "Toggle Tab Layout", the two rows of tabs become a single scrolling row.

JTabbedPane 2

Lastly, if you had stretched the original window, you would have seen this.

JTabbedPane 3

JScrollPanes

In AWT scrolling was built into many of the objects, but in Swing, scrolling is an option you can use with any component, but it isn't built in to any by default.

Basically, when you want something in a scrolling pane, you pass the item you want contained in the scrolling pane to the JScrollPane's constructor. This will "wrap" a scroll pane around whatever you provide.

JScrollPane has

The structure of a JScrollPane is as follows:

JScrollPane Structure
JScrollPane structure

You will typically just add things to the viewport (this is what happens when you pass an object to the constructor). You can also pass values to the constructor to control if you want scrollbars to appear, and if they will always appear or just when needed.

The following code puts the myPanel object into a JScrollPane, which in turn lives inside of a JFrame:

JPanel myPanel = new JPanel();
JScrollPane jsp = new JScrollPane(myPanel);
JFrame jf = new JFrame();
jf.getContentPane().add(jsp)

One interesting feature of a JScrollPane is it's ability to "communicate" with the object it holds. If the object added to a JScrollPane implements the Scrollable interface, the object can tell the JScrollPane how much it wants to scroll when the user clicks areas of the scrollbar, rather than use the default values.

Download and open the Netbeans project jscrollpane. This example shows a few different features. Since the class MainPanel implements Scrollable:

public class MainPanel extends JPanel implements Scrollable

The MainPanel class can tell the scroll pane what size it would like to be. Since the enclosing JFrame calls pack, the window initially comes up sized to hold the component:

JScrollPane

In this example, the yellow rows are actually column and row headers, the "blue" buttons are part of the MainPanel that is scrollable. If you make the window smaller:

JScrollPane scrolling

The scrollbars will appear. Play around with scrolling the window. Try this by dragging the scroll controls and also clicking on the triangle buttons on each bar. Note that the scrolling behavior is different with these "unit increment" controls. The MainPanel class let the scrolling pane know that if it wants to unit increment in the vertical direction, it should do so by the size of each button, instead of its 1 pixel default.

JSplitPane

JSplitPane is a component that is used two divide a space into two separate areas. It allows the user to move the "barrier" to re-allocate space from one area to the other.

This is a very simple class, and the only real notable extra feature is that you can set a "one-touch" expansion/contraction control that will expand/collapse the portion of the split pane with one click.

Download and open the Netbeans project jsplitpane. If you run the application, you will see a window first divided into two vertical sections, with the upper section once again divided into two horizontal section.

Play around with the dividers to get a feel for how they work.

JSplitPane

 

 

Normal">JSplitPane