如何在JSplitPane中获取分隔符组件?

时间:2014-04-23 18:34:02

标签: java swing mouselistener jsplitpane

我想在JSplitPane中获取divider组件。我想将MouseListener附加到它。

如何获取JSplitPane的分隔符组件?

谢谢!

2 个答案:

答案 0 :(得分:3)

  

我想将鼠标监听器附加到它上面。

为什么需要MouseListener(如果我们知道实际需求,可能会有更好的解决方案)?

  

我想在JSplitPane中获取divider组件。

你从UI获得分隔符:

BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
BasicSplitPaneDivider divider = ui.getDivider();

答案 1 :(得分:1)

试试这个 -

最初:

JSplitPane pane = new JSplitPane(JSPlitPane.HORIZONTAL_SPLIT, panelOne, panelTwo);
pane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
        // some processing here
    }
});

更新:

JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelOne, panelTwo);
BasicSplitPaneUI basicSplitPaneUI = (BasicSplitPaneUI) pane.getUI();
BasicSplitPaneDivider basicSplitPaneDivider = basicSplitPaneUI.getDivider();
basicSplitPaneDivider.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseDragged(MouseEvent e) {
        // some processing here
    }
});