Swing JComboBox extension to remove unwanted border

I've spent the last few hours looking for a solution or even a decent guide to this problem, but I couldn't find anything.

I am implementing my own Swing Look and Feel for my little GUI. I've used the UIManager.put("key", values);

method so far with good success, but I'm stuck when it comes to modifying the JComboBoxes correctly.

Using this list, I was able to get my jComboBox closer to how I want them to look:

modified JComboBox

I have two problems with this, major and minor:

  • Main

    enter image description here

    I want the blue border shown to disappear.

  • Minor

    enter image description here

    I would really like the black border shown to disappear.

Apparently, no one key

in UIDefaults

has nothing to do with two boundaries: they seem to be some hard-coded in the variable model Look and Feel (which should be Metal). I resorted to manually extending ComboBoxRenderer and managed to come up with this:

package exec.laf.theme;

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;


public class ComboBoxRenderer extends BasicComboBoxRenderer {
    private Color background;
    private Color selectionBackground;

    public ComboBoxRenderer() {
        super();

        background = UIManager.getColor("ComboBox.background");
        selectionBackground = UIManager.getColor("ComboBox.selectionBackground");
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText((String) value);

        if (isSelected) setBackground(selectionBackground);
        else setBackground(background);

        return this;
    }
}

      

Where do I specify this renderer every time I create a JComboBox like this:

aComboBox.setRenderer(new ComboBoxRenderer());

      

get the same look and feel as the extended JComboBox.

The problem is, with this extension, I can't seem to find a way to touch those boundaries. Adding setBorder(new EmptyBorder(0, 0, 0, 0));

does nothing, as it just adds a border to the listed items.

I checked the source code javax.swing.plaf.basic.BasicComboBoxRenderer

to see if any borders were applied but nothing was found (the only border is the border applied to the listed elements, which I can override as shown above.

What I should do? Am I extending the wrong class, or am I missing something else?

+3


source to share


2 answers


I found a solution:

UIManager.put("ComboBox.borderPaintsFocus", Boolean.TRUE)

      



This sets a boolean value inside the ComboBoxUI that prevents the focus border from being displayed, which is the border drawn around all buttons when they are focused. Its style depends on your appearance.

0


source


To remove the black border of the comboBox PopUp,



Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
popup.setBorder(BorderFactory.createEmptyBorder());

      

0


source







All Articles