How to customize the look of JComboBox?

I want to create a dropdown that looks like this:

alt text http://i44.tinypic.com/v80z81.png

JComboBox provides the functionality I need, so I don’t need to write a custom component. But I can't find a way to customize the whole look of JComboBox to achieve this. Any suggestions?

Refresh . I don't want all JComboBoxes to look like this. I just want to create it with this usual look. So, as far as I can tell, creating custom L & F is not a solution.

+3
source share
2 answers

JComboBox UI , . , L & F Synth L & F.

L & F:

SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();

try {
    lookAndFeel.load(YourClassAlongWithSynthXml.class.getResourceAsStream("synth.xml"), YourClassAlongWithSynthXml.class);
} catch (ParseException e) {
    e.printStackTrace();
}

UIManager.setLookAndFeel(lookAndFeel);

synth.xml - :

<style id="textfield">
    <insets top="4" left="6" bottom="4" right="6" />

    <state>
        <font name="Verdana" size="12" />

        <!--
        <color type="BACKGROUND" value="#D2DFF2" />
        -->
        <color type="TEXT_FOREGROUND" value="#003c2d" />
    </state>

    <imagePainter method="textFieldBorder" path="images/textfield.png" sourceInsets="4 6 4 6" paintCenter="false" />
</style>

<bind style="textfield" type="region" key="TextField" />

<style id="arrowStyle">
    <imagePainter method="arrowButtonForeground" path="images/arrow-up.png" sourceInsets="0 0 0 0" stretch="false" direction="north" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-down.png" sourceInsets="0 0 0 0" stretch="false" direction="south" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-left.png" sourceInsets="0 0 0 0" stretch="false" direction="west" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-right.png" sourceInsets="0 0 0 0" stretch="false" direction="east" />
</style>

<bind key="ArrowButton" type="region" style="arrowStyle" />

<style id="comboArrowStyle">
    <imagePainter method="arrowButtonForeground" path="images/combobox-arrow.png" sourceInsets="0 0 0 0" stretch="false" direction="south" />
</style>

Synth L & F , , .

, L & F , :

final Color COLOR_BUTTON_BACKGROUND = Color.decode("#d3dedb");

UIManager.put("ComboBox.buttonBackground", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonShadow", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonDarkShadow", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonHighlight", COLOR_BUTTON_BACKGROUND);
+8

, jcombobox paintComponent , .

, .

, updateUI, , VM , .

+1

Source: https://habr.com/ru/post/1709650/


All Articles