S-JIS[2009-11-06/2009-11-07] 変更履歴

GroupLayoutUtil

ひしだま作の“定型的なグループレイアウト(GroupLayout)”を作成するユーティリティーです。→ソース

コンポーネントの二次元配列からグループレイアウトを生成します。


最も単純な例

コンポーネントの二次元配列全てに何らかのコンポーネントが設定されている例です。

構造イメージ:

テキスト1
テキスト2

コーディング例:

import java.awt.Component;
import java.awt.Container;
import javax.swing.*;

import jp.hishidama.swing.layout.GroupLayoutUtil;
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ new JLabel("テキスト1"), new JTextField("text1"), new JButton("ボタンA") },
			{ new JLabel("テキスト2"), new JTextField("text2"), new JButton("ボタンB") },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

GroupLayoutUtilのインスタンスを作った後に
setComponents()でコンポーネントの二次元配列をセットし、
setGroupLayoutTo()を呼び出すことでコンテナにグループレイアウトが設定されます。

GroupLayoutにギャップ等の設定を行いたい場合は、以下のようにします。

GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
groupLayoutUtil.setComponents(compos);
groupLayoutUtil.setGroupLayoutTo(container);


GroupLayout layout = groupLayoutUtil.getGroupLayout();

// コンポーネント同士の間隔を空ける設定
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil() {
	@Override
	protected void initGroupLayout(GroupLayout layout) {
		// コンポーネント同士の間隔を空ける設定
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
	}
};
groupLayoutUtil.setComponents(compos);
groupLayoutUtil.setGroupLayoutTo(container);

一部のコンポーネントをセットしない例

コンポーネントの二次元配列の一部にnullをセットすると、そこは何もない状態になります。

構造イメージ:

長い見出しを指定    
テキスト1
テキスト2  

コーディング例:

	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ new JLabel("長い見出しを指定")                                                 },
			{ new JLabel("テキスト1"),       new JTextField("text1"), new JButton("ボタンA") },
			{ new JLabel("テキスト2"),       null                   , new JButton("ボタンB") },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

二次元配列内にnullを指定すると、その位置には何も出力されません。
また、一部の行で列が足りない場合もnullとして扱われます。


セルを結合する例

上記の例では、テキストボックスの開始位置が長い見出しの終了後になっています。
セルを結合することにより、見出しの終了位置を無視することが出来ます。

構造イメージ:

長い見出しを指定  
テキスト1
テキスト2  

コーディング例:

import jp.hishidama.swing.layout.GroupLayoutUtil;
import static jp.hishidama.swing.layout.GroupLayoutUtil.SAME_L;
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ new JLabel("長い見出しを指定"), SAME_L                                          },
			{ new JLabel("テキスト1"),        new JTextField("text1"), new JButton("ボタンA") },
			{ new JLabel("テキスト2"),        null                   , new JButton("ボタンB") },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

SAME_Lは、左と同じコンポーネントが続くことを意味します。


構造イメージ2

1行目ラベル
2行目ラベル

コーディング例2:

	private JComponent line1Label    = new JLabel("1行目ラベル");
	private JComponent line1Text     = new JTextField("テキストボックス");
	private JComponent line1Button11 = new JButton("ボタン1-1");
	private JComponent line1Button12 = new JButton("ボタン1-2");

	private JComponent line2Label   = new JLabel("2行目ラベル");
	private JComponent line2List    = new JScrollPane(new JList(new String[] {"選択肢1", "選択肢2"}));
	private JComponent line2Button2 = new JButton("ボタン2");
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ line1Label, line1Text, line1Button11, line1Button12 },
			{ line2Label, line2List, SAME_L       , line2Button2  },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

GroupLayoutUtilを使わない、素のGroupLayoutでのコーディング方法


同様にSAME_Uは上と同じコンポーネントを表します。

構造イメージ:

リスト1
 
 
テキスト

コーディング例:

import static jp.hishidama.swing.layout.GroupLayoutUtil.SAME_U;
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ new JLabel("リスト1"),  new JScrollPane(new JList(new Object[]{"AA","BB"})), new JButton("ボタン1") },
			{ null,                   SAME_U,                                              new JButton("ボタン2") },
			{ null,                   SAME_U,                                              new JButton("ボタン3") },
			{ new JLabel("テキスト"), new JTextField("text"),                              new JButton("ボタン4") },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

構造イメージ:

リスト1
 
 
テキスト

コーディング例:

import static jp.hishidama.swing.layout.GroupLayoutUtil.*;
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ new JLabel("リスト1"),  new JScrollPane(new JList(new Object[]{"AA","BB"})), SAME_L,                  new JButton("ボタン1") },
			{ null,                   SAME_U,                                              SAME_L,                  new JButton("ボタン2") },
			{ null,                   SAME_U,                                              SAME_L,                  new JButton("ボタン3") },
			{ new JLabel("テキスト"), new JTextField("text1"),                             new JTextField("text2"), new JButton("ボタン4") },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

ギャップを指定する例

ギャップ(コンポーネントとコンポーネントの間の間隔)を指定する例です。[2009-11-07]

構造イメージ:

1行目ラベル
2行目ラベル

※『テキストボックス』と『ボタン1-1』の間を詰める

コーディング例:

import jp.hishidama.swing.layout.GroupLayoutUtil;
import static jp.hishidama.swing.layout.GroupLayoutUtil.*;
	private JComponent line1Label    = new JLabel("1行目ラベル");
	private JComponent line1Text     = new JTextField("テキストボックス");
	private JComponent line1Button11 = new JButton("ボタン1-1");
	private JComponent line1Button12 = new JButton("ボタン1-2");

	private JComponent line2Label   = new JLabel("2行目ラベル");
	private JComponent line2List    = new JScrollPane(new JList(new String[] {"選択肢1", "選択肢2"}));
	private JComponent line2Button2 = new JButton("ボタン2");
	private void initPane(Container container) {

		// 作りたい構造
		Component[][] compos = {
			{ line1Label, line1Text, new Gap(line1Button11 ,0, -1), line1Button12 },
			{ line2Label, line2List, SAME_L                       , line2Button2  },
		};

		GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil() {
			@Override
			protected void initGroupLayout(GroupLayout layout) {
				// コンポーネント同士の間隔を空ける設定
				layout.setAutoCreateGaps(true);
				layout.setAutoCreateContainerGaps(true);
			}
		};
		groupLayoutUtil.setComponents(compos);
		groupLayoutUtil.setGroupLayoutTo(container);
	}

GroupLayoutUtilを使わない、素のGroupLayoutでのコーディング方法

GapクラスはGroupLayoutUtil内に定義されたクラスで、第2引数に横間隔のギャップ、第3引数に縦間隔のギャップを指定します。
GroupLayoutの配置時に、第1引数で指定されたコンポーネントの左側あるいは上側にギャップを追加します。
負の数を指定するとギャップを追加しません。


自作Swingライブラリーへ戻る / 自作ソフトへ戻る / 技術メモへ行く
メールの送信先:ひしだま