Java:JComboBox - > ScrollBar - 改变颜色

时间:2015-07-23 11:55:52

标签: java swing jcombobox jscrollbar

我正在处理小型应用程序。我做了一个JComboBox,我想改变里面的颜色。我已经改变了背景,前景,箭头按钮的颜色......但我不知道如何改变里面的scrollBar的颜色。

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;
    }             
});

我试过了:

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();
                                }

                            }

                      }

1 个答案:

答案 0 :(得分:0)

Hej jakub,以下代码将改变ComboBox内ScrollBar的背景:

编辑:您可以通过以下方式访问滚动条的颜色:

UIManager.put("ScrollBar.thumbHighlight", Color...;         
UIManager.put("ScrollBar.thumbDarkShadow", Color...; 
UIManager.put("ScrollBar.highlight", Color...; 
UIManager.put("ScrollBar.trackHighlight", Color...;