In this tutorials, we are going to implement a simple Java project using Swing JSplitPane and JList.

Swing JSplitPane :

SplitPaneDemo.java
package com.swing.examples;

import java.awt.Container;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SplitPaneDemo extends JFrame {

    JSplitPane splitPane, mainSplitPane;
    JList list;
    JPanel listPanel, imagePanel;
    JLabel img;
    Container contentPane;
    
    String listData[] = {"duke","duke.running","duke2","dukeMagnify","dukeplug","dukeSnooze","dukeWave","dukeWaveRed","cow"};
    
    JTextArea textArea;
    
    SplitPaneDemo(){
        super("SplitPane Demo !");
        list = new JList(listData);
        list.addListSelectionListener(new ListSelectionListener() {
            
            @Override
            public void valueChanged(ListSelectionEvent e) {    
                textArea.setText("This is : "+list.getSelectedValue());
                //Local Images path
                img.setIcon(new ImageIcon("C:\\images\\"+list.getSelectedValue()+".gif"));
            }
        });
        
        textArea = new JTextArea(4,30);
        
        // Adding ListPanel
        listPanel = new JPanel();
        listPanel.add(list);
        
        // Adding Image Pannel
        img = new JLabel();
        imagePanel = new JPanel();
        imagePanel.add(img);
        
        // Creating SplitPanel
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,new JScrollPane(listPanel),imagePanel);
        splitPane.setDividerSize(10);
        splitPane.setOneTouchExpandable(true);
        mainSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitPane,new JScrollPane(textArea));
        mainSplitPane.setDividerSize(10);
        mainSplitPane.setDividerLocation(150);
        contentPane = getContentPane();
        contentPane.add(mainSplitPane);
        
        setSize(400, 275);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    public static void main(String[] args) {
        new SplitPaneDemo();
    }

}

Output :

Spring JSplitPane Example

Selecting the JList and performing an action.

Spring JSplitPane Example 2

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment