Tuesday , November 21 2017
Home / java / Java Swing JSplitPane Example

Java Swing JSplitPane Example

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

Swing JSplitPane :


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 action.

Spring JSplitPane Example 2

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

How to add dynamic files to JTree

In the previous tutorial, we learn about a simple JTree. This tutorial is for adding …

Leave a Reply

Your email address will not be published. Required fields are marked *