• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBoxのItemを左右にクリップして配置
#navi(../)
RIGHT:Posted by [[aterai]] at 2005-09-12
*JComboBoxのItemを左右にクリップして配置 [#td747fe0]
JComboBoxのItemにテキストをクリップして左右に分けて配置します。
---
category: swing
folder: ClippedLRComboBox
title: JComboBoxのItemを左右にクリップして配置
tags: [JComboBox, ListCellRenderer, JLabel, JPanel]
author: aterai
pubdate: 2005-09-12T13:00:56+09:00
description: JComboBoxのItem内のレイアウトをメインとサブの二つに分割し、それぞれ適当な長さに省略した文字列を表示します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJSTVvNXI/AAAAAAAAAUI/RNbSh6R4xi8/s800/ClippedLRComboBox.png
hreflang:
    href: https://java-swing-tips.blogspot.com/2008/08/multi-column-jcombobox.html
    lang: en
---
* 概要 [#summary]
`JComboBox`の`Item`内のレイアウトをメインとサブの二つに分割し、それぞれ適当な長さに省略した文字列を表示します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJSTVvNXI/AAAAAAAAAUI/RNbSh6R4xi8/s800/ClippedLRComboBox.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTJSTVvNXI/AAAAAAAAAUI/RNbSh6R4xi8/s800/ClippedLRComboBox.png)
* サンプルコード [#sourcecode]
#code(link){{
class MultiColumnCellRenderer extends JPanel implements ListCellRenderer {
  private final JLabel leftLabel = new JLabel();
  private final JLabel rightLabel;

**サンプルコード [#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) {
  public MultiColumnCellRenderer(int rightWidth) {
    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));
    this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));

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

    final Dimension dim = new Dimension(rightWidth, 0);
    rightLabel = new JLabel() {
      @Override public Dimension getPreferredSize() {
        return dim;
      }
    };
    rightLabel.setOpaque(false);
    rightLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
    rightLabel.setForeground(Color.GRAY);
    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;

  @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;
      }
    leftLabel.setFont(list.getFont());
    rightLabel.setFont(list.getFont());

    if (index < 0) {
      leftLabel.setForeground(list.getForeground());
      this.setOpaque(false);
    } else {
      leftLabel.setForeground(
          isSelected ? list.getSelectionForeground() : list.getForeground());
      this.setBackground(
          isSelected ? list.getSelectionBackground() : list.getBackground());
      this.setOpaque(true);
    }
    return this;
  }
  private int oldwidth = -1;

  @Override public Dimension getPreferredSize() {
    Dimension d = super.getPreferredSize();
    return new Dimension(0, d.height);
  }

  @Override public void updateUI() {
    super.updateUI();
    this.setName("List.cellRenderer");
  }
}
}}

**解説 [#y1b479cc]
上記のサンプルでは、JLabelを二つ並べたJPanelをレンダラーにすることで、Itemに設定した文字列を左右に表示しています。このため文字列が長い場合、JLabelがこれを自動的にクリップしてくれます。
* 解説 [#explanation]
- `JPanel`に`JLabel`を二つ並べて配置したセルレンダラーを作成し、`Item`に設定した文字列を左右の`JLabel`に表示
- このため左右の文字列が各`JLabel`の推奨サイズより長い場合は自動的に省略表示になる

**参考リンク [#w60ad6dd]
-[[JComboBoxのItemを左右に配置>Swing/LRComboBox]]
--こちらはhtmlのtableタグを使用して同様の表示(クリップはしない)を行っています。
* 参考リンク [#reference]
- [[JComboBoxのItemを左右に配置>Swing/LRComboBox]]
-- こちらは`html`の`table`タグを使用して同様の表示(クリップはしない)を行っている
- [[JComboBoxのドロップダウンリストとしてJTableを使用する>Swing/DropdownTableComboBox]]
-- こちらは`JList`の代わりに`JTable`を使用している

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

#comment