Home » Desktop Java » swing » JSplitPane » Create JSplitPane example

About Byron Kiourtzoglou

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.

Create JSplitPane example

This is an example on how to create a new JSplitPane component for a Java Desktop Application.The JSplitPane is commonly used component because it lets you split your window horizontally or vertically in order to create a wide variety of GUI elements to suit your application’s needs.

In short in order to create a JSplitPane component in Java, one should 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 theJSplitPane.
  • Create two JScrollPane components.
  • Create a new JSplitPane with the above JScrollPane components in each side.
  • Use frame.getContentPane().add(splitPane) to add the spilt pane to your frame
package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;
import java.awt.Panel;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class CreateJSplitPaneExample {

	private static void createAndShowGUI() {

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

		// Display the window.
		frame.setSize(200, 200);
		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" };
		JComboBox combo1 = new JComboBox(options1);

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

		Panel panel1 = new Panel();
		panel1.add(combo1);

		Panel panel2 = new Panel();
		panel2.add(combo2);

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
		// JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);

		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(); 

}

  });
    }

}

This was an example on how to create a JSplitPane component for a Java Desktop Application.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of