检测JSplitPane分割器移动

时间:2013-01-22 21:50:29

标签: java swing jsplitpane

有没有办法检测何时移动JSplitPane分隔线?有没有办法为分隔符移动添加侦听器?

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
// What do I put here to be notified if the divider in sp is moved?

2 个答案:

答案 0 :(得分:23)

我认为您正在寻找Container的{​​{3}}。像这样......

sp.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, 
    new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent pce) {}
});

答案 1 :(得分:5)

使用

splitter.addPropertyChangeListener("dividerLocation", myListener);