• 追加された行はこの色です。
  • 削除された行はこの色です。
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 MultiColumnCellRenderer extends JPanel implements ListCellRenderer {
  private final JLabel leftLabel  = new JLabel();
  private final JLabel rightLabel = new JLabel();
  private int prevwidth = -1;

  public MultiColumnCellRenderer(int rightWidth) {
    super(new BorderLayout());
    this.setOpaque(true);
    this.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
    this.setName("List.cellRenderer");

    leftLabel.setOpaque(false);
    leftLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));

    rightLabel.setOpaque(false);
    rightLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
    rightLabel.setForeground(Color.GRAY);
    rightLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    rightLabel.setPreferredSize(new Dimension(rightWidth, 0));

    this.add(leftLabel);
    this.add(rightLabel, BorderLayout.EAST);
  }
  @Override public Component getListCellRendererComponent(JList list,
      Object value, int index, boolean isSelected, boolean cellHasFocus) {
  @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.setFont(list.getFont());
    rightLabel.setFont(list.getFont());

    leftLabel.setForeground(
        isSelected?list.getSelectionForeground():list.getForeground());
    this.setBackground(
        isSelected?list.getSelectionBackground():list.getBackground());

    if(index<0) {
      Dimension d = getSize();
      if(d.width!=prevwidth) {
        list.setPreferredSize(new Dimension(d.width, 0));
        prevwidth = d.width;
      }
      leftLabel.setForeground(list.getForeground());
      this.setOpaque(false);
    }else{
      leftLabel.setForeground(
          isSelected?list.getSelectionForeground():list.getForeground());
      this.setOpaque(true);
      this.setBackground(
          isSelected?list.getSelectionBackground():list.getBackground());
    }
    return this;
  }
  @Override public void updateUI() {
    prevwidth = -1;
    super.updateUI();
    this.setName("List.cellRenderer");
  }
}
}}

**解説 [#y1b479cc]
上記のサンプルでは、JLabelを二つ並べたJPanelをレンダラーにすることで、Itemに設定した文字列を左右に表示しています。このため文字列が長い場合、JLabelがこれを自動的にクリップしてくれます。

**参考リンク [#w60ad6dd]
-[[JComboBoxのItemを左右に配置>Swing/LRComboBox]]
--こちらはhtmlのtableタグを使用して同様の表示(クリップはしない)を行っています。

**コメント [#n7dbe41e]
- ポップアップリストが更新されなくなって?、うまくクリップできなくなっていたのを修正。 -- [[aterai]] &new{2008-08-13 (水) 15:14:12};
- 選択時の文字色を修正(Windows 7 などへの対応)。 -- [[aterai]] &new{2012-02-03 (金) 14:28:48};

#comment