Swing/TabbedPaneWithCheckBox のバックアップ差分(No.4)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TabbedPaneWithCheckBox へ行く。
- 1 (2006-04-03 (月) 01:35:12)
- 2 (2006-11-17 (金) 11:13:29)
- 3 (2007-08-29 (水) 17:37:27)
- 4 (2008-06-16 (月) 22:32:38)
- 5 (2008-06-20 (金) 11:51:47)
- 6 (2011-05-12 (木) 23:42:43)
- 7 (2013-03-11 (月) 18:33:49)
- 8 (2013-04-06 (土) 20:04:48)
- 9 (2013-09-06 (金) 15:31:15)
- 10 (2014-11-25 (火) 03:03:31)
- 11 (2015-02-04 (水) 18:52:16)
- 12 (2015-03-19 (木) 16:26:09)
- 13 (2017-01-20 (金) 13:21:29)
- 14 (2017-12-16 (土) 20:28:34)
- 15 (2018-04-16 (月) 18:54:10)
- 16 (2020-04-09 (木) 14:40:48)
- 17 (2021-10-13 (水) 02:46:43)
- 18 (2023-11-15 (水) 09:34:41)
- 19 (2024-02-02 (金) 12:18:09)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTabbedPaneの余白にJCheckBoxを配置 #navi(../) *JTabbedPaneの余白にJCheckBoxを配置 [#jf5f1d6a] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-04-03~ 更新日:&lastmod; Posted by [[terai]] at 2006-04-03 #contents **概要 [#mac165bc] JTabbedPaneの余白にJCheckBoxを配置して特定のタブの開閉を行います。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#hd0302da] #code{{ class TabbedPaneWithCompBorder implements Border, MouseListener, SwingConstants { private final JComponent dummy = new JPanel();; private final JCheckBox cbox; private final JTabbedPane tab; private Rectangle rect; public TabbedPaneWithCompBorder(JCheckBox cbox, JTabbedPane tab) { this.cbox = cbox; this.tab = tab; tab.addMouseListener(this); cbox.setFocusPainted(false); cbox.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { JCheckBox cb = (JCheckBox)me.getSource(); cb.setSelected(!cb.isSelected()); } }); } public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { Dimension size = cbox.getPreferredSize(); int xx = tab.getSize().width - size.width; Rectangle lastTab = tab.getUI().getTabBounds(tab, tab.getTabCount()-1); int tabEnd = lastTab.x + lastTab.width; if(xx<tabEnd) xx = tabEnd; rect = new Rectangle(xx, -2, size.width, size.height); SwingUtilities.paintComponent(g, cbox, dummy, rect); } public Insets getBorderInsets(Component c) { return new Insets(0,0,0,0); } public boolean isBorderOpaque() { return true; } private void dispatchEvent(MouseEvent me) { if(rect==null || !rect.contains(me.getX(), me.getY())) return; Point pt = me.getPoint(); pt.translate(0, 0); cbox.setBounds(rect); cbox.dispatchEvent(new MouseEvent( cbox, me.getID(), me.getWhen(), me.getModifiers(), pt.x, pt.y, me.getClickCount(), me.isPopupTrigger(), me.getButton())); } public void mouseClicked(MouseEvent me) { dispatchEvent(me); } public void mouseEntered(MouseEvent me) { dispatchEvent(me); } public void mouseExited(MouseEvent me) { dispatchEvent(me); } public void mousePressed(MouseEvent me) { dispatchEvent(me); } public void mouseReleased(MouseEvent me) { dispatchEvent(me); } } class TabbedPaneWithCompBorder implements Border, MouseListener, SwingConstants { private final JComponent dummy = new JPanel();; private final JCheckBox cbox; private final JTabbedPane tab; private Rectangle rect; public TabbedPaneWithCompBorder(JCheckBox cbox, JTabbedPane tab) { this.cbox = cbox; this.tab = tab; tab.addMouseListener(this); cbox.setFocusPainted(false); cbox.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { JCheckBox cb = (JCheckBox)me.getSource(); cb.setSelected(!cb.isSelected()); } }); } public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { Dimension size = cbox.getPreferredSize(); int xx = tab.getSize().width - size.width; Rectangle lastTab = tab.getUI().getTabBounds(tab, tab.getTabCount()-1); int tabEnd = lastTab.x + lastTab.width; if(xx<tabEnd) xx = tabEnd; rect = new Rectangle(xx, -2, size.width, size.height); SwingUtilities.paintComponent(g, cbox, dummy, rect); } public Insets getBorderInsets(Component c) { return new Insets(0,0,0,0); } public boolean isBorderOpaque() { return true; } private void dispatchEvent(MouseEvent me) { if(rect==null || !rect.contains(me.getX(), me.getY())) return; Point pt = me.getPoint(); pt.translate(0, 0); cbox.setBounds(rect); cbox.dispatchEvent(new MouseEvent( cbox, me.getID(), me.getWhen(), me.getModifiers(), pt.x, pt.y, me.getClickCount(), me.isPopupTrigger(), me.getButton())); } public void mouseClicked(MouseEvent me) { dispatchEvent(me); } public void mouseEntered(MouseEvent me) { dispatchEvent(me); } public void mouseExited(MouseEvent me) { dispatchEvent(me); } public void mousePressed(MouseEvent me) { dispatchEvent(me); } public void mouseReleased(MouseEvent me) { dispatchEvent(me); } } }} -&jnlp; -&jar; -&zip; **解説 [#md0e801a] JTabbedPaneのBorderにSwingUtilities.paintComponentメソッドを使ってJCheckBoxを描画しています。JCheckBoxのイベントが伝播しないようにダミーパネルを中間コンテナに指定しています。 タブとチェックボックスが重ならないように、フレームの最小サイズを設定しています。 **参考リンク [#ze3b3f0b] -[[JTabbedPaneの余白に文字列を表示>Swing/TabbedPaneWithText]] **コメント [#n00381b0] #comment