Swing/ClippedLRComboBox のバックアップの現在との差分(No.22)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- 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`内のレイアウトをメインとサブの二つに分割し、それぞれ適当な長さに省略した文字列を表示します。 #download(https://lh5.googleusercontent.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; public MultiColumnCellRenderer(int rightWidth) { super(new BorderLayout()); 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; leftLabel.setText(item.getLeftText()); rightLabel.setText(item.getRightText()); 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; } @Override public Dimension getPreferredSize() { Dimension d = super.getPreferredSize(); return new Dimension(0, d.height); } @Override public void updateUI() { super.updateUI(); this.setName("List.cellRenderer"); } } }} * 解説 [#explanation] 上記のサンプルでは、`JLabel`を二つ並べた`JPanel`をレンダラーにすることで`Item`に設定した文字列を左右に表示しています。このため文字列が`JLabel`の推奨サイズより長い場合、自動的にクリップされます。 - `JPanel`に`JLabel`を二つ並べて配置したセルレンダラーを作成し、`Item`に設定した文字列を左右の`JLabel`に表示 - このため左右の文字列が各`JLabel`の推奨サイズより長い場合は自動的に省略表示になる * 参考リンク [#reference] - [[JComboBoxのItemを左右に配置>Swing/LRComboBox]] -- こちらは`html`の`table`タグを使用して同様の表示(クリップはしない)を行っている - [[JComboBoxのドロップダウンリストとしてJTableを使用する>Swing/DropdownTableComboBox]] -- こちらは`JList`の代わりに`JTable`を使用している * コメント [#comment] #comment - ポップアップリストが更新されなくなって?、うまくクリップできなくなっていたのを修正。 -- &user(aterai); &new{2008-08-13 (水) 15:14:12}; - 選択時の文字色を修正(`Windows 7`などへの対応)。 -- &user(aterai); &new{2012-02-03 (金) 14:28:48}; #comment