【问题标题】:Change background color editable JComboBox更改背景颜色可编辑的 JComboBox
【发布时间】:2012-05-02 17:55:50
【问题描述】:

我正在编写一个 JFrame 表单中的可编辑组合框,但我想更改背景颜色。

程序的工作原理: 如果我点击按钮“按下”,那么他的背景组合框需要变成黑色。

我试过了:

1.

cbo.setBackground(Color.BLACK);

但它什么也没做

2

cbo.getEditor().getEditorComponent().setBackground(Color.BLACK);

((JTextField) cbo.getEditor().getEditorComponent()).setOpaque(true);

这样做:

代码示例:

public class NewJFrame extends javax.swing.JFrame {

    private JComboBox cboCategorie;

    public NewJFrame() {
        initComponents();

        cboCategorie = new JComboBox();
        cboCategorie.setBounds(10, 10, 250, 26);
        cboCategorie.setVisible(true);
        cboCategorie.setEditable(true);
        this.add(cboCategorie);

    }

private void pressActionPerformed(java.awt.event.ActionEvent evt) {
        cboCategorie.getEditor().getEditorComponent().setBackground(Color.BLACK);
        ((JTextField) cboCategorie.getEditor().getEditorComponent()).setOpaque(true);
}

我正在使用 Java JDK7

有什么建议吗?

【问题讨论】:

  • 你能解决你的问题吗?
  • 不,问题永远无法解决。如果您有解决方案,请随时分享 :) 问题是针对学校项目的,我从未解决过...
  • 相关,可能重复,无法确定:stackoverflow.com/questions/4412902/…

标签: java swing jcombobox java-7 setbackground


【解决方案1】:

结果很容易。创建渲染器并设置背景颜色。然后重写渲染器的 setOpaque 方法,始终将不透明度设置为 true。

class PDFChooser extends JComboBox<String>  {
    PDFChooser() {
        setRenderer(new Renderer());
    }
    class Renderer extends DefaultListCellRenderer {
        @Override 
        public void setOpaque(boolean makeBackGroundVisible) {
            super.setOpaque(true);     // THIS DOES THE TRICK
        }
        @Override
        public Component getListCellRendererComponent(JList<?> list, 
                    Object value, int index, 
                    boolean isSelected, boolean cellHasFocus) {
            setText((String)value);
            setBackground(Color.cyan);
            return this;
        }
    }
}

如果您希望弹出菜单使用 LAF 背景,您可以添加一个 PopupMenuListener 来记录弹出菜单何时弹出。那时,setOpaque 方法会将不透明度设置为 false。

【讨论】:

    【解决方案2】:

    它可以改变 JComboBox 中选定项目的颜色。

        JComboBox cmb = new JComboBox();
        cmb.setEditable(true);
        cmb.setEditor(new WComboBoxEditor(getContentPane().getBackground()));
    
        // To change the arrow button's background        
        cmb.setUI(new BasicComboBoxUI(){
            protected JButton createArrowButton()
            {
                BasicArrowButton arrowButton = new BasicArrowButton(BasicArrowButton.SOUTH, null, null, Color.GRAY, null);
                return arrowButton;
            }
        });
        cmb.setModel(new DefaultComboBoxModel(new String[] { "a", "b", "c" }));
    


    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionListener;
    import javax.swing.ComboBoxEditor;
    import javax.swing.JTextField;
    
    public class WComboBoxEditor implements ComboBoxEditor
    {
        JTextField tf;
    
        public WComboBoxEditor(Color background)
        {
            tf = new JTextField();
            tf.setBackground(background);
            tf.setBorder(null);
        }
    
        public Component getEditorComponent()
        {
            return tf;
        }
    
        public void setItem(Object anObject)
        {
            if (anObject != null)
            {
                tf.setText(anObject.toString());
            }
        }
    
        public Object getItem()
        {
            return tf.getText();
        }
    
        public void selectAll()
        {
            tf.selectAll();
        }
    
        public void addActionListener(ActionListener l)
        {
            tf.addActionListener(l);
        }
    
        public void removeActionListener(ActionListener l)
        {
            tf.removeActionListener(l);
        }
    
    }
    


    如果您想更改 JCombobox 中项目的颜色(除了选定的项目),请自定义 ListCellRenderer。

    【讨论】:

      【解决方案3】:

      查看我的代码示例

      import java.awt.*;
      import java.util.Vector;
      import javax.swing.*;
      import javax.swing.UIManager;
      import javax.swing.plaf.ColorUIResource;
      import javax.swing.plaf.metal.MetalComboBoxButton;
      
      public class MyComboBox {
      
          private Vector<String> listSomeString = new Vector<String>();
          private JComboBox someComboBox = new JComboBox(listSomeString);
          private JComboBox editableComboBox = new JComboBox(listSomeString);
          private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
          private JFrame frame;
      
          public MyComboBox() {
              listSomeString.add("-");
              listSomeString.add("Snowboarding");
              listSomeString.add("Rowing");
              listSomeString.add("Knitting");
              listSomeString.add("Speed reading");
      //
              someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
              someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
              someComboBox.setEditable(true);
              someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
              ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
      //
              editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
              editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
              editableComboBox.setEditable(true);
              JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
              text.setBackground(Color.YELLOW);
              JComboBox coloredArrowsCombo = editableComboBox;
              Component[] comp = coloredArrowsCombo.getComponents();
              for (int i = 0; i < comp.length; i++) {// hack valid only for Metal L&F
                  if (comp[i] instanceof MetalComboBoxButton) {
                      MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                      coloredArrowsButton.setBackground(null);
                      break;
                  }
              }
      //
              non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
              non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
      //
              frame = new JFrame();
              frame.setLayout(new GridLayout(0, 1, 10, 10));
              frame.add(someComboBox);
              frame.add(editableComboBox);
              frame.add(non_EditableComboBox);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setLocation(100, 100);
              frame.pack();
              frame.setVisible(true);
          }
      
          public static void main(String[] args) {
              UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
              UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
              UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
              UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
              SwingUtilities.invokeLater(new Runnable() {
      
                  @Override
                  public void run() {
                      MyComboBox aCTF = new MyComboBox();
                  }
              });
          }
      }
      

      【讨论】:

      • 如果我错了,请纠正我。当我在 JFrame 表单中工作时,您正在从 Java 主类运行它。您的代码正在运行,但是如果我将您的代码复制到新的 JFrame 表单(带有必要的更新),然后运行该文件。没有颜色。我需要做一些特别的事情来让它工作吗?
      • @morpheus05 需要 Java7 中的一些 woodoo 用于 UIManager 中的键
      • @mKorbel 我复制了你的代码,添加了 nimbus LAF 并运行程序。我没看出任何区别。 OP 询问了 nimbus(屏幕截图),所以我认为这个示例适用于 nimbus 和/或 Java 7。
      猜你喜欢
      • 2021-02-08
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 2020-06-12
      • 2011-04-29
      • 2018-12-27
      • 1970-01-01
      • 2013-04-22
      相关资源
      最近更新 更多