Swing/ComponentList のバックアップ差分(No.5)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2004-03-22~ 更新日:&lastmod; #contents **概要 [#k994c8ea] 高さの異なるコンポーネントをスクロールできるようにリスト状に並べます。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#gefca96e] #code{{ private final Box pnl = Box.createVerticalBox(); private final Vector list = new Vector(); public void addComp(JComponent c) { list.addElement(c); pnl.removeAll(); for(int i=0;i<list.size();i++) { JComponent tmp = (JComponent)list.get(i); 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(); } tmp.setMaximumSize(new Dimension(Integer.MAX_VALUE, d.height)); pnl.add(tmp); } pnl.add(Box.createVerticalGlue()); pnl.revalidate(); } }} -&jnlp; -&jar; -&zip; // public void addComp(JComponent label) { // SpringLayout layout = new SpringLayout(); // Component[] list = pnl.getComponents(); // if(list.length==0) { // layout.putConstraint( // SpringLayout.WEST,label,0,SpringLayout.WEST,pnl); // layout.putConstraint( // SpringLayout.NORTH,label,0,SpringLayout.NORTH,pnl); // }else{ // JComponent cmp = null; // for(int i=0;i<list.length;i++) { // JComponent tmp = (JComponent)list[i]; // layout.putConstraint( // SpringLayout.WEST,tmp,0,SpringLayout.WEST,pnl); // if(cmp==null) { // layout.putConstraint( // SpringLayout.NORTH,tmp,0,SpringLayout.NORTH,pnl); // }else{ // layout.putConstraint( // SpringLayout.NORTH,tmp,0,SpringLayout.SOUTH,cmp); // } // cmp = tmp; // } // layout.putConstraint( // SpringLayout.WEST,label,0,SpringLayout.WEST,pnl); // layout.putConstraint( // SpringLayout.NORTH,label,0,SpringLayout.SOUTH,cmp); // } // pnl.add(label); // pnl.setLayout(layout); // initComps(); // } // // public void initComps() { // Rectangle rv = scroll.getViewport().getViewRect(); // Insets ins = pnl.getInsets(); // int cw = (int)rv.getWidth() - ins.left - ins.right; // int ch = 0; // Component[] list = pnl.getComponents(); // for(int i=0;i<list.length;i++) { // JComponent tmp = (JComponent)list[i]; // int th = tmp.getPreferredSize().height; // tmp.setPreferredSize(new Dimension(cw, th)); // ch = ch + th; // } // Dimension d = new Dimension( // (int)rv.getWidth(),ch+ins.top+ins.bottom); // pnl.setPreferredSize(d); // pnl.revalidate(); // } // **解説 // 上記のサンプルでは、SpringLayoutを使ってパネルにコンポーネントを並べています。右クリックのポップアップメニューから、高さの異なるラベルとボタンを追加することができます。追加されたコンポーネントの幅はJListのようにViewportに合わせて変化しますが、高さはsetPreferredSize()で設定されたものを維持します。 // // チュートリアルの[[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メソッドは以下のように書くこともできます。 // 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(); // } **解説 [#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