JSplitPane

Get/Set divider size in JSplitPane

This is an example on how to get or set the size of the divider in a JSplitPane component of a Java Desktop Application.

In order to do that you have to follow these steps:

  • Create a new JFrame.
  • Call frame.getContentPane().setLayout(new FlowLayout()) to set flow layout for the frame.
  • Create two String arrays that will containt the contents of the two areas of the JSplitPane.
  • Create two JScrollPane components.
  • Create a new JSplitPane with the above JScrollPane components in each side.
  • Call splitPane.getDividerSize() to get the divider size.
  • Call splitPane.setDividerSize() to set the divider size.

Let’s see the code snippet that follows.

package com.javacodegeeks.snippets.desktop;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class GetSetDividerSizeInJSplitPane {

	private static void createAndShowGUI() {

		// Create and set up the window.
		final JFrame frame = new JFrame("Split Pane Example");

		// Display the window.
		frame.setSize(500, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// set flow layout for the frame
		frame.getContentPane().setLayout(new FlowLayout());

		String[] options1 = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
		JList list1 = new JList(options1);

		String[] options2 = { "Car", "Motorcycle", "Airplane", "Boat" };
		JList list2 = new JList(options2);

		JScrollPane scrollPane1 = new JScrollPane(list1);
		JScrollPane scrollPane2 = new JScrollPane(list2);

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane1, scrollPane2);
		splitPane.setPreferredSize(new Dimension(400, 200));

		// get the current divider size (pixels)
		int dividerLocation = splitPane.getDividerSize();

		System.out.println("Divider Size before: " + dividerLocation);

		// set new divider size
		splitPane.setDividerSize(5);

		System.out.println("Divider Size after: " + splitPane.getDividerSize());

		frame.getContentPane().add(splitPane);

	}

	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

Output:

Divider Size before: 10 Divider Size after: 2

This was an example on how to get and set the divider size in JSplitPane.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button