JComboBox changing drop down arrow image not working

actunderdc :

I am trying to change the dropdown arrow for a JComboBox with a custom image, but the code doesn't seem to work. I followed instructions from here. I tried with both Java 1.8.131 and AdoptOpenJDK 11.0.5, but the result is the same for both of them. Below you can find the full code that I am using:

Main.java

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { 
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    JFrame f = new JFrame("Window");
    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 400, 400);
    panel.setBackground(Color.gray);

    JComboBox<String> box = new JComboBox<>();//simple JComboBox with custom UI
    box.setPreferredSize(new Dimension(150, 30));
    box.setUI(MyBasicComboboxUI.createUI(box));

    box.addItem("BasicComboBoxUI1");
    box.addItem("BasicComboBoxUI2");
    box.addItem("BasicComboBoxUI3");

    panel.add(box);

    JButton btn = new JButton("SimpleJButton");//simple JButton with an image on it (to prove that the image loads)
    btn.setPreferredSize(new Dimension(120, 30));
    MyImageProvider imageProvider = new MyImageProvider();
    btn.setIcon(new ImageIcon(imageProvider.getImage()));
    btn.setBorder(new EmptyBorder(0, 0, 0, 0));

    panel.add(btn);
    f.add(panel);
    f.setSize(400, 400);
    f.setLayout(null);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

MyBasicComboboxUI.java:

public class MyBasicComboboxUI extends BasicComboBoxUI {
    public static ComboBoxUI createUI(JComponent c) {
        return new MyBasicComboboxUI();
    }

    @Override
    protected JButton createArrowButton() {
        JButton arrowButton = super.createArrowButton();
        MyImageProvider imgProvider = new MyImageProvider();
        arrowButton.setSize(new Dimension(40, 40));
        arrowButton.setToolTipText("My tooltip");
        arrowButton.setIcon(new ImageIcon(imgProvider.getImage()));//set the same icon here
        arrowButton.setBorder(new EmptyBorder(0, 0, 0, 0));
        return arrowButton;
    }
}

MyImageProvider.java

public class MyImageProvider {

    public Image getImage() {
        try {
            Image img = ImageIO.read(getClass().getResource("/icons/arrow.gif"));
            return img;
        } catch (IOException e) {
            System.out.println("The image was not loaded.");
        }
        return null;
    }
}

Image used 12px x 12px: arrow.gif

Image at 24px x 24px: enter image description here

Output when running the program:

enter image description here

The tooltip set on the arrow is working. The same if I use a custom background color. But not if I set an image. I tried with: *.jpg, *.gif, *.png and different resolutions: 16x16, 14x14, 12x12, 8x8 etc. but with no success. In all cases the image loaded only on the SimpleJButton, but not on the drop down arrow button of the combobox.

Eclipse structure:

enter image description here

Sergiy Medvynskyy :

Your problem is the line

JButton arrowButton = super.createArrowButton();

you should change it to

JButton arrowButton = new JButton();

Background: super.createArrowButton() return an instance of the ArrowButton class, that provides custom arrow painting and does not support setIcon method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322416&siteId=1