Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement JSplitPane into a Frame in Java using Swing API?.

A Simple program demonstrating the working of JSplitPane in Java using Swing framework.

package com.hubberspot.swing.example;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplitPaneDemo extends JFrame {

  static String text = "This is a simple program that demonstrate " + 
    "the use of JSplitPane which is a simple split pane which help " +
    "us in dividing two text areas and giving us a way to put to " + 
    "sample text side by side.";

  public SplitPaneDemo() {
    
    JTextArea textarea1 = new JTextArea(text);
    JTextArea textarea2 = new JTextArea(text);

    textarea1.setLineWrap(true);
    textarea2.setLineWrap(true);
    
    textarea1.setMinimumSize(new Dimension(150, 150));
    textarea2.setMinimumSize(new Dimension(150, 150));
    
    textarea1.setPreferredSize(new Dimension(250, 200));
    JSplitPane splitpane = 
     new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, textarea1, textarea2);
    getContentPane().add(splitpane, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    setFrame(new SplitPaneDemo(), 500, 400);
  }

  public static void 
  setFrame(final JFrame frame, final int width, final int height)
  {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
 frame.setTitle(frame.getClass().getSimpleName());
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(width, height);
 frame.setVisible(true);
      }
    });
  }
}


Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot