检测 JSplitPane 分隔器移动

2022-09-03 04:31:01

有没有办法检测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?

答案 1

我想你正在寻找 addPropertyChangeListener from .像这样的东西...Container

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

答案 2

splitter.addPropertyChangeListener("dividerLocation", myListener);

推荐