2013-01-15 34 views
5

我想要一个没有箭头按钮(完成)的JComboBox,它在启用时具有绿色背景,当禁用(未完成)时具有灰色背景。我也使用自定义渲染器的下拉列表(完成)在Swing中设置JComboBox的背景

我检查了BasicComboBoxUI的源代码,并试图重写一些方法,但没有任何反应。下拉菜单始终有灰色/蓝色背景。

这是我最后一次尝试的SSCCE。我尝试了所有我能想到的。请给我一个提示,我迷路了。

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Rectangle; 

import javax.swing.BorderFactory; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.plaf.basic.BasicComboBoxUI; 

public class DropDownBackground 
{ 
    public static void main(final String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 

      public void run() 
      { 
       final JComboBox dropdown = new JComboBox(new DefaultComboBoxModel(new String[] { "one", "two", "three" })); 
       dropdown.setRenderer(new ComboBoxListCellRenderer()); 
       dropdown.setUI(new BasicComboBoxUI() 
       { 
        @Override 
        public void paint(final Graphics g, final JComponent c) 
        { 

         final Rectangle r = this.rectangleForCurrentValue(); 
         this.paintCurrentValueBackground(g, r, true); 
         this.paintCurrentValue(g, r, true); 

        } 

        @Override 
        public void paintCurrentValueBackground(final Graphics g, final Rectangle bounds, final boolean hasFocus) 
        { 
         final Color t = g.getColor(); 
         if (this.comboBox.isEnabled()) 
          g.setColor(Color.GREEN); 
         else 
          g.setColor(Color.GRAY); 
         g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); 
         g.setColor(t); 
        } 

        @Override 
        protected JButton createArrowButton() 
        { 
         return new JButton() 
         { 
          @Override 
          public int getWidth() 
          { 
           return 0; 
          } 
         }; 
        } 
       }); 
       dropdown.setBackground(Color.GREEN); 
       final JPanel p = new JPanel(); 
       p.add(dropdown); 

       final JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.getContentPane().add(new JScrollPane(p)); 
       f.setSize(800, 200); 
       f.setLocation(0, 0); 

       f.setVisible(true); 

      } 
     }); 

    } 

    public static class ComboBoxListCellRenderer extends DefaultListCellRenderer 
    { 
     /** 
     * 
     */ 
     private static final long serialVersionUID = 1L; 

     @Override 
     public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) 
     { 
      this.setToolTipText((String) value); 
      if (isSelected) 
      { 
       this.setBackground(Color.RED); 
       this.setForeground(Color.WHITE); 
      } 
      else 
      { 
       this.setBackground(Color.WHITE); 
       this.setForeground(Color.BLACK); 
      } 

      this.setText((String) value); 
      this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 

      return this; 
     } 
    } 

} 

回答

7

因为我想有这个颜色的应用广泛,这是最好的办法:

UIManager.put("ComboBox.background", new ColorUIResource(UIManager.getColor("TextField.background"))); 
UIManager.put("ComboBox.foreground", new ColorUIResource(UIManager.getColor("TextField.foreground"))); 
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.GREEN)); 
UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.WHITE)); 

如果你想定制更(禁用颜色等等)这个UIManager属性列表可能是有用的:http://www.rgagnon.com/javadetails/JavaUIDefaults.txt

+0

你是对的。 (1) – Spiff

0

为改变JComboBox背景颜色我会检查这个线程:http://www.coderanch.com/t/555124/GUI/java/JComboBox-background-colour-customer-renderer

编辑: 说实话,我懒得看你的代码,但是这个怎么样? http://www.coderanch.com/t/343835/GUI/java/custom-renderer-JComboBox

似乎更渲染面向:)

+0

在您的链接之后,我发现了一些关于JComboBox.setEditor()的信息。我创建了一个自定义编辑器并设置了可编辑的组合框。然而,这种方法奏效,我觉得这是一个糟糕的解决方案,因为我使用了不可编辑的编辑器,并且必须将ComboBox设置为可编辑,实际上它不可编辑。这是现在的一个快速解决方案,但进一步的帮助表示赞赏。 有关自定义编辑器的信息可以在这里找到:http://www.coderanch.com/t/333756/GUI/java/creating-custom-ComboBoxEditor – haferblues

+0

检查编辑请:) –