एक JSplitPane, JComponent क्लास का एक सबक्लास है जो हमें दो कंपोनेंट्स को एक ही पेन में हॉरिजॉन्टली या वर्टिकली साइड-बाय-साइड व्यवस्थित करने की अनुमति देता है। दोनों कंपोनेंट्स के डिस्प्ले एरिया को रनटाइम पर यूजर द्वारा एडजस्ट भी किया जा सकता है। JSplitPane के महत्वपूर्ण मेथड्स हैं: remove(), removeAll(), resetToPreferredSizes() और setDividerLocation()। एक JSplitPane PropertyChangeListener इंटरफेस उत्पन्न कर सकता है। हम लेफ्ट बटन या राइट बटन पर क्लिक करके प्रोग्रामेटिकली पेन में से एक (लेफ्ट या राइट) को छुपा सकते हैं और उन बटन्स के लिए एक्शन लिसनर्स उत्पन्न कर सकते हैं।
उदाहरण
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSplitPaneHideTest extends JFrame {
private JButton leftBtn, rightBtn;
private JSplitPane jsp;
public JSplitPaneHideTest() {
setTitle(" JSplitPaneHide Test");
leftBtn = new JButton("Left Button");
rightBtn = new JButton("Right Button");
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftBtn, rightBtn);
jsp.setResizeWeight(0.5); // Implemention code to hide left pane or right pane
ActionListener actionListener = new ActionListener() {
private int loc = 0;
public void actionPerformed(ActionEvent ae) {
JButton source = (JButton)ae.getSource();
if(jsp.getLeftComponent().isVisible() && jsp.getRightComponent().isVisible()) {
loc = jsp.getDividerLocation();
jsp.setDividerSize(0);
jsp.getLeftComponent().setVisible(source == leftBtn);
jsp.getRightComponent().setVisible(source == rightBtn);
} else {
jsp.getLeftComponent().setVisible(true);
jsp.getRightComponent().setVisible(true);
jsp.setDividerLocation(loc);
jsp.setDividerSize((Integer) UIManager.get("SplitPane.dividerSize"));
}
}
};
rightBtn.addActionListener(actionListener);
leftBtn.addActionListener(actionListener);
add(jsp, BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new JSplitPaneHideTest();
}
}
आउटपुट


