Bitcoin

Bitcoin
Bitcoin

JSplitPane Java

A JSplitPane is a splitter that can be used to split space between two components. The splitter bar can be displayed horizontally or vertically. When the available space is less than the space needed to display the two components, the user can move the splitter bar up/down or left/right, so one
component gets more space than the other. If there is enough space, both components can be shown fully. The JSplitPane class provides many constructors. we can create it using its default constructor and add two components using its 
setTopComponent(Component c), setBottomComponent(Component c),setLeftComponent(Component c), setRightComponent(Component c).

JSplitPane can redraw components in continuous or non-continuous way. when we change the position of the splitter bar.

Reset the divider position to that position by calling the resetToPreferredSizes() method of JSplitPane.

Divider Location

Change the dividerLocation property with setDividerLocation(newLocation). 0.0 and 1.0, representing a percentage of the JSplitPane container width.
import java.awt.BorderLayout;
/*from   www.sjavaspot.com*/
import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String[] a) { JFrame horizontalFrame = new JFrame(); horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent topButton = new JButton("Left"); JComponent bottomButton = new JButton("Right"); final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(topButton); splitPane.setBottomComponent(bottomButton); horizontalFrame.add(splitPane, BorderLayout.CENTER); horizontalFrame.setSize(150, 150); horizontalFrame.setVisible(true); splitPane.setDividerLocation(0.5); } }

Expandable Divider

Resizing Components and Working with a One-Touch Expandable Divider
import java.awt.BorderLayout;
/*from   www.sjavaspot.com*/
import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Property Split"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setContinuousLayout(true); splitPane.setOneTouchExpandable(true); JComponent topComponent = new JButton("A"); splitPane.setTopComponent(topComponent); JComponent bottomComponent = new JButton("B"); splitPane.setBottomComponent(bottomComponent); frame.add(splitPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }

Nested JSplitPane

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
/*from   www.sjavaspot.com*/
public class Main{ public static void main(String[] a) { int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT; int VERTSPLIT = JSplitPane.VERTICAL_SPLIT; boolean continuousLayout = true; JLabel label1 = new JLabel("a"); JLabel label2 = new JLabel("b"); JLabel label3 = new JLabel("c"); JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, label1, label2); splitPane1.setOneTouchExpandable(true); splitPane1.setDividerSize(2); splitPane1.setDividerLocation(0.5); JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, label3); splitPane2.setOneTouchExpandable(true); splitPane2.setDividerLocation(0.4); splitPane2.setDividerSize(2); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(splitPane2); frame.pack(); frame.setVisible(true); } }

Listening for JSplitPane Property Changes

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
/*from   www.sjavaspot.com*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class Main {
   static JTextArea area = new JTextArea(30, 40);
  public static void main(String args[]) {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new GridLayout(2, 1));
    
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);



    JComponent topComponent = new JButton("A");
    p.add(topComponent);
    //splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    p.add(bottomComponent);
    splitPane.setBottomComponent(p);

    splitPane.setTopComponent(new JScrollPane(area));

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent changeEvent) {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
          int current = sourceSplitPane.getDividerLocation();
          Integer last = (Integer) changeEvent.getNewValue();
          Integer priorLast = (Integer) changeEvent.getOldValue();
          area.setText("Current: "+current+"\nLast: "+last+"\nPrior last: "+priorLast);
        }
      }
    };

    splitPane.addPropertyChangeListener(propertyChangeListener);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





No comments:

Post a Comment

Facebook