Swing/ClippedLRComboBox のバックアップソース(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ClippedLRComboBox へ行く。
- 1 (2005-09-12 (月) 13:00:56)
- 2 (2005-09-12 (月) 23:29:51)
- 3 (2006-02-27 (月) 15:33:00)
- 4 (2006-04-08 (土) 20:51:26)
- 5 (2006-04-12 (水) 19:37:05)
- 6 (2006-07-07 (金) 12:26:23)
- 7 (2007-01-11 (木) 14:20:21)
- 8 (2007-09-16 (日) 00:28:21)
- 9 (2008-06-17 (火) 17:50:19)
- 10 (2008-08-13 (水) 15:12:54)
- 11 (2011-03-13 (日) 01:49:33)
- 12 (2012-02-03 (金) 14:26:04)
- 13 (2012-02-03 (金) 16:02:16)
- 14 (2013-03-24 (日) 21:29:51)
- 15 (2014-11-28 (金) 16:24:34)
- 16 (2015-03-01 (日) 16:08:08)
- 17 (2016-05-26 (木) 14:35:58)
- 18 (2017-08-12 (土) 21:43:45)
- 19 (2018-02-24 (土) 19:51:30)
- 20 (2018-04-02 (月) 17:50:03)
- 21 (2020-03-31 (火) 13:50:03)
- 22 (2021-10-08 (金) 17:41:41)
- 23 (2023-07-21 (金) 17:12:28)
TITLE:JComboBoxのItemを左右にクリップして配置 #navi(../) RIGHT:Posted by [[aterai]] at 2005-09-12 *JComboBoxのItemを左右にクリップして配置 [#td747fe0] JComboBoxのItemにテキストをクリップして左右に分けて配置します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTJSTVvNXI/AAAAAAAAAUI/RNbSh6R4xi8/s800/ClippedLRComboBox.png) **サンプルコード [#n26d00a1] #code{{ class LRComboCellRenderer extends JPanel implements ListCellRenderer { private final JLabel leftLabel = new JLabel(); private final JLabel rightLabel = new JLabel(); private final Color cfc = UIManager.getColor("ComboBox.foreground"); private final Color cbc = UIManager.getColor("ComboBox.background"); private final Color csfc = UIManager.getColor("ComboBox.selectionForeground"); private final Color csbc = UIManager.getColor("ComboBox.selectionBackground"); private final Color cdfc = UIManager.getColor("ComboBox.disabledForeground"); private final JComboBox combo; public LRComboCellRenderer(JComboBox combo, int rightWidth, int labelHeight) { super(new BorderLayout()); this.combo = combo; this.setBorder(BorderFactory.createEmptyBorder()); this.setOpaque(true); leftLabel.setOpaque(true); leftLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0)); rightLabel.setOpaque(true); rightLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,2)); rightLabel.setPreferredSize(new Dimension(rightWidth, labelHeight)); rightLabel.setHorizontalAlignment(SwingConstants.RIGHT); this.add(leftLabel); this.add(rightLabel, BorderLayout.EAST); } //@Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { LRItem item = (LRItem)value; leftLabel.setText(item.getLeftText()); rightLabel.setText(item.getRightText()); leftLabel.setBackground(isSelected? csbc:cbc); rightLabel.setBackground(isSelected? csbc:cbc); this.setBackground(isSelected? csbc:cbc); leftLabel.setForeground(isSelected? csfc:cfc); rightLabel.setForeground(cdfc); if(index==-1) { Dimension dim = combo.getSize(); if(dim.width!=oldwidth) { int count = combo.getItemCount()-1; Insets i = combo.getInsets(); int w = dim.width-i.left-i.right; int h = dim.height-i.top-i.bottom; list.setPreferredSize(new Dimension(w, h*count)); oldwidth = dim.width; } } return this; } private int oldwidth = -1; } }} **解説 [#y1b479cc] 上記のサンプルでは、JLabelを二つ並べたJPanelをレンダラーにすることで、Itemに設定した文字列を左右に表示しています。このため文字列が長い場合、JLabelがこれを自動的にクリップしてくれます。 **参考リンク [#w60ad6dd] -[[JComboBoxのItemを左右に配置>Swing/LRComboBox]] --こちらはhtmlのtableタグを使用して同様の表示(クリップはしない)を行っています。 **コメント [#n7dbe41e] - ポップアップリストが更新されなくなって?、うまくクリップできなくなっていたのを修正。 -- [[aterai]] &new{2008-08-13 (水) 15:14:12}; #comment