I'm working on small application. I made a JComboBox, and I want to change the colours inside. I already changed colour for background, foreground, arrowButton, ... But I don't know how to change the colours for scrollBar inside.
comoBox = new JComboBox();
comoBox.setName("currencyCombo");
DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
comboModel.addElement("gagD");
comboModel.addElement("agg");
comboModel.addElement("EhgsY");
comboModel.addElement("CgafgY");
comboModel.addElement("CfadgY");
comboModel.addElement("Ggafg");
comboModel.addElement("Sgfsdg");
comboModel.addElement("Ugfasdg");
comboModel.addElement("fasfasf");
comoBox.setModel(comboModel);
comoBox.setOpaque(true);
UIManager.put( "ComboBox.disabledBackground", new java.awt.Color(32, 34, 41));
UIManager.put( "ComboBox.disabledForeground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.background", new java.awt.Color(32, 34, 41));
UIManager.put( "ComboBox.foreground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.selectionBackground", new java.awt.Color(181, 181, 181));
UIManager.put( "ComboBox.selectionForeground", new java.awt.Color(32, 34, 41));
comoBox.setUI(new BasicComboBoxUI(){
@Override
protected JButton createArrowButton() {
JButton arrowButton = new BasicArrowButton(
BasicArrowButton.SOUTH,
new java.awt.Color(32, 34, 41),
new java.awt.Color(181, 181, 181),
new java.awt.Color(181, 181, 181),
new java.awt.Color(32, 34, 41));
arrowButton.setBorder(BorderFactory.createLineBorder(new java.awt.Color(32, 34, 41)));
return arrowButton;
}
});
I tried this:
Object obejct = currencyCombo.getUI().getAccessibleChild(currencyCombo, 0);
Component co = ((Container) obejct).getComponent(0) ;
if( co instanceof JScrollPane){
JScrollPane pane = (JScrollPane) co;
Component[] components = pane.getComponents();
for (Component component : components) {
if(component instanceof JScrollBar)
{
JScrollBar jsb = (JScrollBar) component;
jsb.setBackground(Color.red);
jsb.setForeground(Color.blue);
jsb.setUI(new BasicScrollBarUI());
jsb.updateUI();
}
}
}
Hej jakub, the following code will change the background of ScrollBar inside the ComboBox:
EDIT: You can access to the color of the Scrollbar in these ways:
UIManager.put("ScrollBar.thumbHighlight", Color...;
UIManager.put("ScrollBar.thumbDarkShadow", Color...;
UIManager.put("ScrollBar.highlight", Color...;
UIManager.put("ScrollBar.trackHighlight", Color...;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.