• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JComboBoxのItemを左右にクリップして配置 [#td747fe0]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-09-12~
更新日:&lastmod;

#contents

**概要 [#p068c669]
JComboBoxのItemにテキストをクリップして左右に分けて配置します。

#screenshot

**サンプルコード [#n26d00a1]
 class MyCellRenderer extends JPanel implements ListCellRenderer {
   private final JLabel lbl1 = new JLabel();
   private final JLabel lbl2 = new JLabel();
   public MyCellRenderer(Dimension dim, int rightw) {
     super(new BorderLayout());
     lbl1.setOpaque(true);
     lbl2.setOpaque(true);
     this.setOpaque(true);
     lbl1.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
     lbl2.setPreferredSize(new Dimension(rightw, 0));
     lbl2.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
     this.add(lbl1, BorderLayout.CENTER);
     this.add(lbl2, BorderLayout.EAST);
     this.setPreferredSize(dim);
   }
   public Component getListCellRendererComponent(
                     JList list, Object value, int index,
                     boolean isSelected, boolean cellHasFocus) {
     LRItem item = (LRItem)value;
     lbl1.setText(item.getLeftText());
     lbl2.setText(item.getRightText());
     lbl1.setBackground(isSelected ? SystemColor.textHighlight :
                                     Color.white);
     lbl2.setBackground(isSelected ? SystemColor.textHighlight :
                                     Color.white);
     this.setBackground(isSelected ? SystemColor.textHighlight :
                                     Color.white);
     lbl1.setForeground(isSelected ? Color.white :
                                     Color.black);
     lbl2.setForeground(isSelected ? Color.gray.brighter() :
                                     Color.gray);
     return this;
   }
 }

-&jnlp;
-&jar;
-&zip;

**解説 [#y1b479cc]
上記のサンプルは、[[JComboBoxのItemを左右に配置>Swing/LRComboBox]]と同じように、Itemに設定した文字列を左右に表示しますが、htmlのtableタグは使用せず、JLabelを二つ並べたJPanelをレンダラーとして使用しています。このため文字列が長い場合、JLabelがこれを自動的にクリップして表示します。
[[JComboBoxのItemを左右に配置>Swing/LRComboBox]]と同様に、Itemに設定した文字列を左右に表示しますが、htmlのtableタグは使用せず、JLabelを二つ並べたJPanelをレンダラーとして使用しています。このため文字列が長い場合、JLabelがこれを自動的にクリップして表示します。

**参考リンク [#w60ad6dd]
-[[JComboBoxのItemを左右に配置>Swing/LRComboBox]]

**コメント [#n7dbe41e]
#comment