jsplitpane
The JSplitPane
class in Swing provides a way to split a component into two parts, so that the user can adjust the size of each part. This is useful for situations where you have two views of the same data, or for showing a preview pane alongside a main content area.
Here's an example of how to create and use a JSplitPane
in Java:
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; public class SplitPaneExample { public static void main(String[] args) { // Create some content for the left and right sides of the split pane JPanel leftPanel = new JPanel(); JLabel leftLabel = new JLabel("Left side"); leftPanel.add(leftLabel); JPanel rightPanel = new JPanel(); JLabel rightLabel = new JLabel("Right side"); rightPanel.add(rightLabel); // Create the split pane with the left and right content JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel); // Set the initial size of the split pane splitPane.setDividerLocation(150); // Create a frame to hold the split pane JFrame frame = new JFrame("SplitPane Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the split pane to the frame and show it frame.add(splitPane); frame.pack(); frame.setVisible(true); } }Source:wwitfigi.wdea.com
In this example, we create two JPanel
objects to represent the left and right sides of the split pane. We then create a JSplitPane
object, passing in the two panels as arguments, and set its orientation to JSplitPane.HORIZONTAL_SPLIT
. We also set the initial position of the divider between the two panels to 150 pixels.
Finally, we create a JFrame
object to hold the split pane, add the split pane to the frame, and display the frame. When you run this program, you should see a window with a horizontal split pane, with the text "Left side" on the left and "Right side" on the right. You can drag the divider to adjust the size of each panel.