Swing/ComponentList のバックアップソース(No.6)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ComponentList へ行く。
- 1 (2006-06-26 (月) 15:13:35)
- 2 (2006-06-27 (火) 11:25:11)
- 3 (2006-11-24 (金) 15:52:46)
- 4 (2007-08-30 (木) 14:45:12)
- 5 (2009-05-15 (金) 18:59:43)
- 6 (2009-05-15 (金) 22:54:28)
- 7 (2013-02-20 (水) 15:53:16)
- 8 (2013-02-28 (木) 14:34:59)
- 9 (2013-10-16 (水) 14:27:06)
- 10 (2015-11-01 (日) 22:31:34)
- 11 (2016-09-06 (火) 13:43:17)
- 12 (2017-04-04 (火) 14:17:08)
- 13 (2017-10-15 (日) 20:13:17)
- 14 (2018-10-20 (土) 19:09:16)
- 15 (2020-10-18 (日) 18:44:46)
- 16 (2022-08-02 (火) 16:37:39)
TITLE:BoxLayoutでリスト状に並べる #navi(../) RIGHT:Posted by [[terai]] at 2004-03-22 *BoxLayoutでリスト状に並べる [#n63d4aaf] 高さの異なるコンポーネントをスクロールできるようにリスト状に並べます。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#gefca96e] #code{{ private final Box pnl = Box.createVerticalBox(); private final Vector<JComponent> list = new Vector<JComponent>(); public void addComp(JComponent c) { list.addElement(c); pnl.removeAll(); for(JComponent tmp: list) { Dimension d = tmp.getPreferredSize(); tmp.setMaximumSize(new Dimension(Integer.MAX_VALUE, d.height)); pnl.add(tmp); } pnl.add(Box.createVerticalGlue()); pnl.revalidate(); } }} **解説 [#b910d821] 上記のサンプルでは、Box.createVerticalBoxを使ってリスト状に並べています。この際、各コンポーネントの高さは変化せず、幅だけフレームサイズに追従するように、JComponent#setMinimumSizeを指定しています。 コンポーネントの高さの合計がフレームの高さより小さい場合は、下部に余白が出来るように、最後にBox.createVerticalGlueを追加しています。 // チュートリアルの[[How to Use SpringLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container)>http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html]]にある[[SpringUtilities.java>http://java.sun.com/docs/books/tutorial/uiswing/layout/examples/SpringUtilities.java]]を使用すると、サンプルのaddCompメソッドは以下のように書くこともできます。 //#code{{ //public void addComp2(JComponent cmp) { // pnl.add(cmp); // Component[] list = pnl.getComponents(); // SpringUtilities.makeCompactGrid(pnl, // list.length, 1, //rows, cols // 6, 6, //initX, initY // 6, 6); //xPad, yPad // initComps(); //} //**参考リンク **コメント [#eec7c049] - SpringLayoutではなく、BoxLayoutを使うようにサンプルを変更しました。 -- [[terai]] &new{2006-06-26 (月) 15:34:41}; #comment