Swing/TabbedPaneWithText のバックアップソース(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TabbedPaneWithText へ行く。
- 1 (2005-12-26 (月) 12:36:53)
- 2 (2006-01-06 (金) 05:07:45)
- 3 (2006-02-27 (月) 16:57:08)
- 4 (2007-03-05 (月) 01:54:21)
- 5 (2007-09-27 (木) 01:20:23)
- 6 (2007-10-23 (火) 17:09:12)
- 7 (2011-05-05 (木) 08:27:15)
- 8 (2012-10-28 (日) 22:32:37)
- 9 (2013-03-18 (月) 19:29:57)
- 10 (2013-03-21 (木) 15:47:25)
- 11 (2013-09-06 (金) 15:31:52)
- 12 (2014-11-25 (火) 03:03:31)
- 13 (2015-02-04 (水) 18:53:38)
- 14 (2015-03-04 (水) 17:19:20)
- 15 (2017-01-20 (金) 13:20:23)
- 16 (2017-12-19 (火) 16:21:48)
- 17 (2019-02-14 (木) 12:45:11)
- 18 (2019-02-18 (月) 13:44:59)
- 19 (2020-12-05 (土) 16:18:33)
- 20 (2023-04-05 (水) 13:41:03)
TITLE:JTabbedPaneの余白に文字列を表示 #navi(../) #tags() RIGHT:Posted by &author(aterai); at 2005-12-26 *JTabbedPaneの余白に文字列を表示 [#d14a047c] JTabbedPaneの右側の余白に文字列を表示します。[https://forums.oracle.com/forums/thread.jspa?threadID=1390495 Swing - JTabbedPane with non-tabbed text]の投稿からソースコードを引用しています。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTUTbAqf_I/AAAAAAAAAl0/APOrWdnvDko/s800/TabbedPaneWithText.png) **サンプルコード [#f6205526] #code(link){{ tab = new JTabbedPane() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); String text = "←ちょっとしたタブの説明など"; FontMetrics fm = getFontMetrics(getFont()); int stringWidth = fm.stringWidth(text)+10; int x = getSize().width-stringWidth; Rectangle lastTab = getUI().getTabBounds(this, getTabCount()-1); int tabEnd = lastTab.x + lastTab.width; if(x<tabEnd) x = tabEnd; g.drawString(text, x+5, 18); } }; }} **解説 [#t35e18f2] JTabbedPane#paintComponentメソッドをオーバーライドして、タブコンポーネントの右側の余白に文字列を描画しています。 右端に十分な余白が無く、文字列を描画するとタブ上に重なってしまう場合は、最後のタブの横から文字列を描画するようになっています。 ---- ``JDK 1.7.0``の``JLayer``を使用する場合は、以下のような方法があります。 #code{{ import java.awt.*; import javax.swing.*; import javax.swing.plaf.*; public class TopRightCornerLabelLayerUITest { public static JComponent makeUI() { JTabbedPane tab = new JTabbedPane(); tab.addTab("New tab1", new JLabel("1")); tab.addTab("New Tab2", new JLabel("2")); JPanel p = new JPanel(new BorderLayout()); p.add(new JLayer<JComponent>(tab, new TopRightCornerLabelLayerUI())); return p; } private static void createAndShowUI() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(makeUI()); f.setSize(320, 240); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { createAndShowUI(); } }); } } class TopRightCornerLabelLayerUI extends LayerUI<JComponent> { private JLabel l = new JLabel("A Label at right corner"); private JPanel rubberStamp = new JPanel(); @Override public void paint(Graphics g, JComponent c) { super.paint(g, c); Dimension d = l.getPreferredSize(); int x = c.getWidth() - d.width - 5; SwingUtilities.paintComponent(g, l, rubberStamp, x, 2, d.width, d.height); } } }} **参考リンク [#w33aebb6] -[https://forums.oracle.com/forums/thread.jspa?threadID=1390495 Swing - JTabbedPane with non-tabbed text] -[[JTabbedPaneの余白にJCheckBoxを配置>Swing/TabbedPaneWithCheckBox]] **コメント [#q1e57b08] #comment