• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JSplitPaneを等分割する
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2010-06-28
*JSplitPaneを等分割する [#mabfea1e]
JSplitPaneのディバイダが中央にくるように設定します。
---
category: swing
folder: DividerLocation
title: JSplitPaneを等分割する
tags: [JSplitPane, Divider]
author: aterai
pubdate: 2010-06-28T22:43:18+09:00
description: JSplitPaneのディバイダが中央にくるように設定します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTLR0Z5M_I/AAAAAAAAAXU/R6r6dvVJa9M/s800/DividerLocation.png
---
* 概要 [#summary]
`JSplitPane`のディバイダが中央にくるように設定します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTLR0Z5M_I/AAAAAAAAAXU/R6r6dvVJa9M/s800/DividerLocation.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTLR0Z5M_I/AAAAAAAAAXU/R6r6dvVJa9M/s800/DividerLocation.png)

**サンプルコード [#mdde9cfa]
* サンプルコード [#sourcecode]
#code(link){{
EventQueue.invokeLater(new Runnable() {
  @Override public void run() {
    sp.setDividerLocation(0.5);
    //sp.setResizeWeight(0.5);
    // sp.setResizeWeight(0.5);
  }
});
}}

**解説 [#p41e1a6c]
上記のサンプルでは、JSplitPaneのサイズが決まった後(例えばJFrame#pack()や、JFrame#setSize(int,int)などした後)で、JSplitPane#setDividerLocation(0.5); を使用し、ディバイダを中央に配置しています。
* 解説 [#explanation]
上記のサンプルの`JSplitPane`は、初期状態でそのディバイダが中央に配置されるよう、`JSplitPane#setDividerLocation(.5);`を設定しています。

- JSplitPane#setDividerLocation(double)
-- ディバイダ自身の幅(JSplitPane#getDividerSize())は含まれない
-- 内部では、切り捨てでJSplitPane#setDividerLocation(int)を使用: [[JSplitPaneのDividerの位置を最大化後に変更する>Swing/DividerSplitRatio]]
- `JFrame#pack()`や`JFrame#setSize(int, int)`などが実行されて親コンポーネントのサイズが決定した後に`JSplitPane#setDividerLocation(...)`メソッドを実行しないと効果がない
- ディバイダ自身の幅(`JSplitPane#getDividerSize()`)は含まれない
- 内部では切り捨てで`JSplitPane#setDividerLocation(int)`を使用: [[JSplitPaneのDividerの位置を最大化後に変更する>Swing/DividerSplitRatio]]
- `JSplitPane#setResizeWeight(double)`を使用して`JSplitPane`内に配置したコンポーネント(`JScrollPane`)の余ったスペース配分を同じにすることでディバイダを中央に配置する方法もある

----
JSplitPane#setResizeWeight(double)を使用し、JSplitPane内に配置したコンポーネント(JScrollPane)の余ったスペースの配分が同じになるようにして、ディバイダを中央に配置する方法もあります。

**参考リンク [#we34ef80]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JSplitPane.html#setDividerLocation-double- JSplitPane#setDividerLocation(double) (Java Platform SE 8)]
- [[JSplitPaneのDividerの位置を最大化後に変更する>Swing/DividerSplitRatio]]

**コメント [#k7e6f9ad]
* コメント [#comment]
#comment
#comment