Java Program to Create a Split Pane with Two Parts

This is a Java Program to Create a Split Pane with Two Parts to Display Button and Display Text inside Text Area when Button is Clicked

Problem Description

We have to write a program in Java such that it creates a vertically split frame where the left part consists of few push buttons and the right part consists of a text area. When a push button is clicked, the button is displayed in the text area.

Expected Input and Output

To create a Split Pane, we have the following sets of input and output.

1. To view the frame:

At the execution of the program,
it is expected that a vertically split frame appears.
left part frame displays 5 push buttons and right part displays text area

2. To view text in the text area:

Suppose there are 5 push buttons in left part of frame 
(Button 1, Button 2, Button 3, Button 4, Button 5)
When any push button is clicked for example (Button 1 is clicked),
then it is expected that the text area shows "Button clicked: Button 1".
Problem Solution

1. Create a frame and two panels for the left part and the right part.
2. Add few buttons to the left part, and a text area to the right part.
3. Create a Split Pane using JSplitPane and add the left and right components to it.
4. Add the Split Pane to the frame and display the frame.

Program/Source Code

Here is source code of the Java Program to create a Split Pane. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

Note: Join free Sanfoundry classes at Telegram or Youtube
  1. /*Java Program to Create a Split Pane*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Split_Pane implements ActionListener
  6. {
  7.     static JTextArea text;
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11.         //Create a frame
  12. 	JFrame frame = new JFrame("Tabbed Pane");
  13. 	frame.setSize(750,250);
  14. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	//Create an object of the class
  16. 	Split_Pane obj = new Split_Pane();
  17. 	//Create Panel 1 for Left Part
  18. 	JPanel panel1 = new JPanel(false);
  19. 	JButton button[]=new JButton[5];
  20. 	for(int i=0;i<5;i++)
  21. 	{
  22. 	    button[i]=new JButton("Button "+(i+1));
  23. 	    button[i].addActionListener(obj);
  24. 	    panel1.add(button[i]);
  25. 	}
  26. 	//Create Panel 2 for Right Part
  27. 	JPanel panel2 = new JPanel(false);
  28. 	text=new JTextArea(5,20);
  29. 	panel2.add(text);
  30. 	//Create a split pane
  31. 	JSplitPane pane = new JSplitPane(SwingConstants.VERTICAL);
  32. 	pane.setLeftComponent(panel1);
  33. 	pane.setRightComponent(panel2);
  34. 	//Add split pane to frame
  35. 	frame.add(pane);
  36. 	//Display the frame
  37. 	frame.setVisible(true);
  38.     }
  39.     //Function to display the button clicked
  40.     public void actionPerformed(ActionEvent e)
  41.     {
  42.         String button=e.getActionCommand();
  43. 	text.setText("Button Clicked : "+button);
  44.     }
  45. }
Program Explanation

1. Panel 1 is the left component of the split pane, and it consists of push buttons.
2. Panel 2 is the right component of the split pane, and it consists of a text area.
3. A vertical split pane is created using JSplitPane. To set the spit orientation to vertical SwingConstants.VERTICAL is used.
4. The left and right components are set using setLeftComponent and setRightComponent respectively.
5. The split pane is added to the frame and the frame is displayed.
6. When a button is clicked, the actionPerformed function is called by the ActionListener and it displays the button clicked in the text area.

Runtime Test Cases

Here’s the run time test cases to create a Split Pane.


Test case 1 – To View the Split Pane

Test case 2 – To View the Button 1 Clicked

Test case 3 – To View the Button 4 Clicked


Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.