• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBoxのItemを左右にクリップして配置
#navi(../)
RIGHT:Posted by [[aterai]] at 2005-09-12
*JComboBoxのItemを左右にクリップして配置 [#td747fe0]
Posted by [[terai]] at 2005-09-12

#contents

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

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

#screenshot
//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTJSTVvNXI/AAAAAAAAAUI/RNbSh6R4xi8/s800/ClippedLRComboBox.png)

**サンプルコード [#n26d00a1]
#code{{
class MyCellRenderer extends JPanel implements ListCellRenderer {
  private final JLabel lbl1 = new JLabel();
  private final JLabel lbl2 = 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");
  public MyCellRenderer(Dimension dim, int rightw) {
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());
    lbl1.setOpaque(true);
    lbl2.setOpaque(true);
    this.combo = combo;
    this.setBorder(BorderFactory.createEmptyBorder());
    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);
    this.add(lbl2, BorderLayout.EAST);
    this.setPreferredSize(dim);
    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);
  }
  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;
    lbl1.setText(item.getLeftText());
    lbl2.setText(item.getRightText());
    lbl1.setBackground(isSelected ? csbc : cbc);
    lbl2.setBackground(isSelected ? csbc : cbc);
    this.setBackground(isSelected ? csbc : cbc);
    lbl1.setForeground(isSelected ? csfc : cfc);
    lbl2.setForeground(cdfc);
    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