Swing/PaddingComboBox のバックアップの現在との差分(No.25)
- category: swing folder: PaddingComboBox title: JComboBoxの内余白 tags: [JComboBox, Border, LookAndFeel, JTextField] author: aterai pubdate: 2007-05-28T05:42:18+09:00 description: JComboBoxのエディタなどに内余白を設定します。 image:
概要
JComboBox
のエディタなどに内余白を設定します。
Screenshot
Advertisement
サンプルコード
static Border padding = BorderFactory.createEmptyBorder(0, 5, 0, 0);
#spandel
//...
#spanend
#spanadd
// ...
#spanend
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
model.addElement("aaaaaaaaaaaaaaaaaaaaaaaaa");
model.addElement("aaaabbb");
model.addElement("aaaabbbcc");
model.addElement("bbb1");
model.addElement("bbb12");
JComboBox<String> combo = new JComboBox<String>(model) {
@Override public void updateUI() {
setRenderer(null);
super.updateUI();
final ListCellRenderer<? super String> lcr = getRenderer();
ListCellRenderer<? super String> lcr = getRenderer();
setRenderer(new ListCellRenderer<String>() {
@Override public Component getListCellRendererComponent(
JList<? extends String> list, String value, int index,
boolean isSelected, boolean hasFocus) {
JLabel l = (JLabel) lcr.getListCellRendererComponent(
list, value, index, isSelected, hasFocus);
l.setBorder(padding);
return l;
}
});
//XXX JDK 1.7.0 ?: ((JLabel) lcr).setBorder(padding);
// XXX JDK 1.7.0 ?: ((JLabel) lcr).setBorder(padding);
}
};
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JComboBox
に、Border
(EmptyBorder
とMatteBorder
を切り替え可能)を設定したListCellRenderer
を設定して、ドロップダウンリストの左余白をすこし広げています。JComboBox
が編集不可の場合、エディタ部分もこの余白が自動的に適用されます。
上記のサンプルでは、JComboBox
にBorder
(EmptyBorder
とMatteBorder
を切り替え可能)を設定したListCellRenderer
を設定して、ドロップダウンリストの左余白をすこし広げています。JComboBox
が編集不可の場合、エディタ部分もこの余白が自動的に適用されます。
0
:編集不可ListCellRenderer
で余白を指定
以下は、
JComboBox
が編集可の場合のテストです。
-
1
: テキストフィールドの元Border
+ 任意のBorder
で余白を設定 -
1
:JComboBox
が編集可 + テキストフィールドの元Border
+ 任意のBorder
で余白を設定editor.setBorder(BorderFactory.createCompoundBorder(editor.getBorder(), padding));
JComboBox#getEditor()#getEditorComponent()
で取得したJTextField
に余白を指定JDK 1.5
では余白を指定しても反映されないJDK 1.6
では取得したJTextFieldをsetOpaque(true)
としないと背景色は反映されない
-
2
: テキストフィールドの元Border
は無視して任意のBorder
のみで余白を設定 -
2
:JComboBox
が編集可 + テキストフィールドの元Border
は無視して任意のBorder
のみで余白を設定editor.setBorder(padding);
JComboBox#getEditor()#getEditorComponent()
で取得したJTextField
に余白を指定MetalLookAndFeel
でテキストフィールドの枠が描画できない
-
3
: テキストフィールドのInsets
+5
ピクセル余白を設定 -
3
:JComboBox
が編集可 + テキストフィールドのInsets
+5
ピクセル余白を設定editor.setMargin(new Insets(i.top,i.left+5,i.bottom,i.right));
-
MetalLookAndFeel
,MotifLookAndFeel
,WindowsLookAndFeel
などでは無効 -
NimbusLookAndFeel
では有効だが、JComboBox
の高さなども変化してしまう -
MetalLookAndFeel
、MotifLookAndFeel
、WindowsLookAndFeel
などでは無効 -
NimbusLookAndFeel
では有効だがJComboBox
の高さなども変化してしまう
-
4
: テキストフィールドのMargin
+5
ピクセル余白を設定 -
4
:JComboBox
が編集可 + テキストフィールドのMargin
+5
ピクセル余白を設定editor.setMargin(new Insets(m.top,m.left+5,m.bottom,m.right));
-
MetalLookAndFeel
,MotifLookAndFeel
,WindowsLookAndFeel
などでは無効 -
MetalLookAndFeel
、MotifLookAndFeel
、WindowsLookAndFeel
などでは無効 NimbusLookAndFeel
では有効
-
5
:JComboBox
のBorder
+ 任意のBorder
で余白をJComboBox
自身に設定-
JComboBox#setBorder()
で、元のBorder
の内側に余白を指定 -
WindowsLookAndFeel
,MotifLookAndFeel
で有効? -
MetalLookAndFeel
,NimbusLookAndFeel
では、JComboBox
の外側に余白が付く
-
-
5
:JComboBox
が編集可 +JComboBox
のBorder
+ 任意のBorder
で余白をJComboBox
自身に設定-
JComboBox#setBorder()
で元のBorder
の内側に余白を指定 -
WindowsLookAndFeel
、MotifLookAndFeel
で有効? -
MetalLookAndFeel
、NimbusLookAndFeel
ではJComboBox
の外側に余白が付く
-
-
6
:JComboBox
のBorder
+ 任意のBorder
で余白をJComboBox
自身に設定-
JComboBox#setBorder()
で、元のBorder
の外側に余白を指定
-
-
6
:JComboBox
が編集可 +JComboBox
のBorder
+ 任意のBorder
で余白をJComboBox
自身に設定-
JComboBox#setBorder()
で元のBorder
の外側に余白を指定 WindowsLookAndFeel
で余計な枠が表示される?
-
UIManager
で余白を設定する方法もありますが、LookAndFeel
によって対応が異なる?ようです。ComboBox.padding
は無くなっている?
JComboBox
が編集可能の場合は、ComboBox.editorBorder
が有効かもしれない。
-
その他にも、以下のようにUIManager
で余白を設定する方法もあるがLookAndFeel
によって対応が異なる?ComboBox.padding
は無くなっている?-
JComboBox
が編集可能の場合はComboBox.editorBorder
が有効かもしれない#spandel //UIManager.put("ComboBox.padding", new InsetsUIResource(insets)); #spanend #spanadd // UIManager.put("ComboBox.padding", new InsetsUIResource(insets)); #spanend UIManager.put("ComboBox.editorBorder", BorderFactory.createEmptyBorder(0, 5, 0, 0));
-
Ubuntu 7.04
(GNOME 2.18.1
)、JDK 1.6.0
で実行すると、以下のようになります。-
上記のサンプルを余白に色無しにしてUbuntu 7.04
(GNOME 2.18.1
)、JDK 1.6.0
で実行すると、以下のように表示される
LookAndFeel
毎にJComboBox
の余白の描画は異なるみたいなので、全部まとめて消すのは難しい?BasicComboBoxUI
も、ComboBox.buttonDarkShadow
がArrowButton
の三角とボタンの影に使われていて微妙BasicComboBoxUI#createArrowButton()
をオーバーライドして別途三角形アイコンを使う方がよさそう- コードは、JComboBoxのBorderを変更するに移動
参考リンク
- JComboBoxにアイコンを表示
- Bug ID: 4515838 Can't change the border of a JComboBox
- JDK-4515838 Can't change the border of a JComboBox - Java Bug System
- JDK-8179027 JComboBox too small under Windows LAF - Java Bug System
- 上記のスクリーンショットのような
WindowsLookAndFeel
でJComboBox
の高さが小さすぎる件が修正される?
- 上記のスクリーンショットのような