• title: JSplitPaneでディバイダの移動を連続的に再描画 tags: [JSplitPane, Divider, PropertyChangeListener] author: aterai pubdate: 2010-07-12T15:30:54+09:00 description: 二つのJSplitPaneのディバイダで、移動の同期と連続的な再描画を行います。

概要

二つのJSplitPaneのディバイダで、移動の同期と連続的な再描画を行います。

サンプルコード

final JSplitPane leftPane   = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
final JSplitPane rightPane  = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
final JSplitPane centerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

leftPane.setTopComponent(new JScrollPane(new JTextArea("aaaaaaa")));
leftPane.setBottomComponent(new JScrollPane(new JTextArea("bbbb")));
rightPane.setTopComponent(new JScrollPane(new JTree()));
rightPane.setBottomComponent(new JScrollPane(new JTree()));
centerPane.setLeftComponent(leftPane);
centerPane.setRightComponent(rightPane);

leftPane.setResizeWeight(.5);
rightPane.setResizeWeight(.5);
centerPane.setResizeWeight(.5);

PropertyChangeListener pcl = new PropertyChangeListener() {
  @Override public void propertyChange(PropertyChangeEvent e) {
    if(JSplitPane.DIVIDER_LOCATION_PROPERTY.equals(e.getPropertyName())) {
      JSplitPane source = (JSplitPane)e.getSource();
      int location = ((Integer)e.getNewValue()).intValue();
      JSplitPane target = (source==leftPane)?rightPane:leftPane;
      if(location != target.getDividerLocation())
          target.setDividerLocation(location);
    }
  }
};
leftPane.addPropertyChangeListener(pcl);
rightPane.addPropertyChangeListener(pcl);
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JSplitPane3つ使用して、4つのコンポーネントを分割表示しています。


左右に配置されたディバイダが、同期して移動するように以下のように設定しています。

  • 片方のディバイダが上下移動したら、残りも移動するようにPropertyChangeListenerを追加
  • JSplitPane#setContinuousLayout(true)として、移動を連続的に再描画

参考リンク

コメント