Reputation: 686
I have a JComboBox, where nearly everything was changed regarding the layout. I changed the selection color in the popup, the arrow button, the background colors etc. The last remaining part, which I am not getting to look the way I want it, is the border color of the popup (the inner JList).
My combo box looks like that:
As you can see, it has a black border, which is not matching to the whole layout. I want to change that to the blue color you see in the border above.
I did some research, but no solution worked for me. The closest solution was this post. But it didn't work.
Along with the changing of the seleciton color, I already tried to change the border color the following way:
public static void setSelectionColorOfComboBox(JComboBox<String> comboBox)
{
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup) child;
JList list = popup.getList();
list.setBorder(new LineBorder(ColorPalette.LIGHT_BLUE, 1));
list.setSelectionBackground(ColorPalette.LIGHT_BLUE);
list.setSelectionForeground(Color.WHITE);
list.setFocusable(false);
}
To make sure not to miss anything, I created a separate method to change that border, according to the linked post from above:
public static void setBorderColorOfComboBoxPopup(JComboBox<String> comboBox)
{
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup) child;
JList list = popup.getList();
list.setBorder(new LineBorder(ColorPalette.LIGHT_BLUE));
}
But this didn't work either. Does anyone know why those common ways don't work for me, or does anyone at least know another approach to change the border color, so I can try something different?
Currently I am thinking about, if there is something like a scroll pane around the JList, and I will do some research about if I can get access to it and set its border, if it's possible.
Upvotes: 2
Views: 1714
Reputation: 686
After hours of trying and researching, my last idea of the scroll pane guided me to the solution in just a couple of minutes. Thinking about a component that is containing the list, I thought about the popup. The popup contains of two levels, which can have a border. The first is the JList from the popup, and the second one is the popup itself. Somehow the black border covered the blue border of my inner JList. Long story short, following code piece did the work:
public static void setBorderColorOfComboBoxPopup(JComboBox<String> comboBox)
{
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup) child;
popup.setBorder(new LineBorder(ColorPalette.LIGHT_BLUE));
// JList list = popup.getList();
// list.setBorder(new LineBorder(ColorPalette.LIGHT_BLUE));
}
Upvotes: 0
Reputation: 2759
You can override the createPopup()
method in the BasicComboBoxUI
class to change the border for the popup:
jComboBox.setUI(new BasicComboBoxUI() {
@Override
protected ComboPopup createPopup() {
BasicComboPopup basicComboPopup = new BasicComboPopup(comboBox);
basicComboPopup.setBorder(new LineBorder(Color.RED));
return basicComboPopup;
}
});
Upvotes: 4