Swing/CardLayoutTabbedPane のバックアップの現在との差分(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CardLayoutTabbedPane へ行く。
- 1 (2008-10-27 (月) 13:54:48)
- 2 (2008-10-28 (火) 14:30:41)
- 3 (2008-10-29 (水) 18:14:02)
- 4 (2009-07-15 (水) 20:53:26)
- 5 (2012-02-10 (金) 16:50:40)
- 6 (2013-04-06 (土) 05:10:09)
- 7 (2013-10-10 (木) 11:37:46)
- 8 (2014-11-28 (金) 16:30:30)
- 9 (2014-12-05 (金) 17:20:09)
- 10 (2015-03-07 (土) 15:40:04)
- 11 (2015-06-25 (木) 23:33:21)
- 12 (2015-06-29 (月) 04:22:34)
- 13 (2017-03-23 (木) 12:16:38)
- 14 (2018-01-26 (金) 22:45:48)
- 15 (2018-02-24 (土) 19:51:30)
- 16 (2020-01-25 (土) 17:03:44)
- 17 (2021-07-18 (日) 06:25:27)
- 18 (2024-02-03 (土) 14:08:07)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:CardLayoutを使ってJTabbedPane風のコンポーネントを作成 #navi(../) #tags(CardLayout, GridLayout, LayoutManager, JRadioButton, JTableHeader, JTabbedPane) RIGHT:Posted by &author(aterai); at 2008-10-27 * CardLayoutを使ってJTabbedPane風のコンポーネントを作成 [#u2e4ee0a] ``CardLayout``と``JRadioButton``や``JTableHeader``を組み合わせて``JTabbedPane``風のコンポーネントを作成します。 --- category: swing folder: CardLayoutTabbedPane title: CardLayoutを使ってJTabbedPane風のコンポーネントを作成 tags: [CardLayout, GridLayout, LayoutManager, JRadioButton, JTableHeader, JTabbedPane, DragAndDrop] author: aterai pubdate: 2008-10-27T13:54:48+09:00 description: CardLayoutとJRadioButtonやJTableHeaderを組み合わせてJTabbedPane風のコンポーネントを作成します。 image: https://lh3.googleusercontent.com/-i_zX5mZNCL0/VZBOp7c2kwI/AAAAAAAAN74/yEHMZL9l8xs/s800/CardLayoutTabbedPane.png hreflang: href: https://java-swing-tips.blogspot.com/2008/11/create-jtabbedpane-like-component-using.html lang: en --- * 概要 [#summary] `CardLayout`と`JRadioButton`や`JTableHeader`を組み合わせて`JTabbedPane`風のコンポーネントを作成します。 - &jnlp; - &jar; - &zip; #download(https://lh3.googleusercontent.com/-i_zX5mZNCL0/VZBOp7c2kwI/AAAAAAAAN74/yEHMZL9l8xs/s800/CardLayoutTabbedPane.png) #ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIbe7vt7I/AAAAAAAAASw/SGFMcgCN_r4/s800/CardLayoutTabbedPane.png) ** サンプルコード [#wd961a26] * サンプルコード [#sourcecode] #code(link){{ class CardLayoutTabbedPane extends JPanel { protected final CardLayout cardLayout = new CardLayout(); protected final JPanel tabPanel = new JPanel(new GridLayout(1, 0, 0, 0)); protected final JPanel wrapPanel = new JPanel(new BorderLayout(0, 0)); protected final JPanel contentsPanel = new JPanel(cardLayout); protected final ButtonGroup bg = new ButtonGroup(); public CardLayoutTabbedPane() { super(new BorderLayout()); int left = 1; int right = 3; tabPanel.setBorder(BorderFactory.createEmptyBorder(1,left,0,right)); contentsPanel.setBorder(BorderFactory.createEmptyBorder(4,left,2,right)); tabPanel.setBorder( BorderFactory.createEmptyBorder(1, left, 0, right)); contentsPanel.setBorder( BorderFactory.createEmptyBorder(4, left, 2, right)); wrapPanel.add(tabPanel); wrapPanel.add(new JLabel("test:"), BorderLayout.WEST); add(wrapPanel, BorderLayout.NORTH); add(contentsPanel); } public void addTab(final String title, final Component comp) { public void addTab(String title, Component comp) { JRadioButton b = new TabButton(new AbstractAction(title) { public void actionPerformed(ActionEvent e) { @Override public void actionPerformed(ActionEvent e) { cardLayout.show(contentsPanel, title); } }); tabPanel.add(b); bg.add(b); b.setSelected(true); contentsPanel.add(comp, title); cardLayout.show(contentsPanel, title); } } }} ** 解説 [#w7802e67] ``CardLayout``で``JTabbedPane``風のコンポーネントを作成すると、タブエリアのレイアウトをネストしてコンポーネントの追加したり、レイアウトマネージャーを変更することで、タブの配置を変更することが簡単になります。 * 解説 [#explanation] - `CardLayout`と`JRadioButton`を使用して`JTabbedPane`風のコンポーネントを作成 -- `CardLayout`でパネルを切り替えるためのタブとして`UI`を変更してチェックアイコンを非表示にした`JRadioButton`を使用 -- これらのタブを配置するタブエリア(`JPanel`)のレイアウトマネージャーに`GridLayout`を適用して、すべてのタブサイズが均等になるように設定 -- `CardLayout`+`JTableHeader`を使用したサンプルは[[JTableHeaderで作成したタブエリアでCardLayoutのコンテナを切り替える>Swing/TableHeaderTabArea]]に移動 - ``CardLayout``+``JRadioButton`` -- 上記のサンプルでは、``JRadioButton``を``GridLayout``で、均等なサイズになるように並べています。 -- ``UI``を変更して、チェックは非表示にしています。 -- マウスでタブを押した時ではなく、 %%(``Opera``風に)%% 放した時に切り替わります。 - ``CardLayout``+``JTableHeader`` -- 空の``JTable``を作成して``JTableHeader``を取り出して利用しています。 -- ``JTableHeader``のドラッグ&ドロップによる入れ替えや、ヘッダ(タブ)幅のリサイズが利用できます。 ** 参考リンク [#w973281a] * 参考リンク [#reference] - [[JTabbedPaneのタブをドラッグ&ドロップ>Swing/DnDTabbedPane]] - [[TabbedPane風のタブ配置をレイアウトマネージャーで変更>Swing/NewTabButton]] - [[JTableHeaderで作成したタブエリアでCardLayoutのコンテナを切り替える>Swing/TableHeaderTabArea]] ** コメント [#i7cab3fd] - ``CloseButton``(ダミー)を追加。 -- [[aterai]] &new{2008-10-29 (水) 18:14:02}; * コメント [#comment] #comment - タブに`CloseButton`を追加。 -- &user(aterai); &new{2008-10-29 (水) 18:14:02}; #comment