问题是分隔器位置是在拆分窗格布局包含的组件之前设置的LayoutManager
。
解决方案非常简单,您只需将调用包装在SwingUtilities.invokeLater
. 另一种方法是向ComponentListener
拆分窗格中包含的每个组件添加一个。
代码的小例子:
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.SwingWorker.StateValue;
import javax.swing.UIManager;
public class TestSplitPaneDragging {
protected void initUI() {
final JFrame frame = new JFrame();
frame.setTitle(TestSplitPaneDragging.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(new JTextArea(24, 48));
splitPane.setRightComponent(new JTextArea(24, 48));
splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.err.println(splitPane.getLeftComponent().getWidth());
}
});
}
});
frame.add(splitPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestSplitPaneDragging().initUI();
}
});
}
}