简体   繁体   中英

Making JComboBox transparent

I have a problem with making JComboBox transparent. I tried setting opaque to false and alpha of background 0 but it doesnt work. I guess that i need to change some class that does rendering or something similar.And here is the code..

  import java.awt.EventQueue;
  import java.awt.Graphics;
  import java.awt.Rectangle;

  import javax.swing.JFrame;
  import javax.swing.JComboBox;
  import javax.swing.JTextField;
  import javax.swing.plaf.basic.BasicComboBoxUI;

  import java.awt.Color;


public class App {

private JFrame frame;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                App window = new App();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public App() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.GREEN);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
    JComboBox comboBox = new JComboBox(petStrings);
    comboBox.setBounds(149, 99, 155, 20);
    comboBox.setOpaque(false);
    //comboBox.setBackground(new Color(0,0,0,0));
    ((JTextField)comboBox.getEditor().getEditorComponent()).setOpaque(false);
    comboBox.setUI(new BasicComboBoxUI(){  

        public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus){}});  
    frame.getContentPane().add(comboBox);

}

}

Assuming you just want the ComboBox's text field transparent (not the popup as well), using the following code should work. You need to mess with the ComboBox renderer instead of the editor. The editor is used for if you can type into the ComboBox; The renderer is used if the ComboBox is a list of values only.

comboBox.setOpaque(false);
comboBox.setRenderer(new DefaultListCellRenderer(){
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        JComponent result = (JComponent)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        result.setOpaque(false);
        return result;
    }});
JComboBox myComboBox = new JComboBox(array);
myComboBox .setOpaque(false);
myComboBox .setEditable(true);
JTextField boxField = (JTextField)myComboBox .getEditor().getEditorComponent();
boxField.setBorder(BorderFactory.createEmptyBorder());
boxField.setBackground(new Color(0, 0, 0, 0));
boxField.setFocusable(false);

The answer is in http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6687960

You need to preset this few things

jcombo.setOpaque(false);
jcombo.setContentAreaFilled(false);
jcombo.setBorderPainted(false);

try this.

yourComboBox.setOpaque(false);
((JTextField)yourComboBox.getEditor().getEditorComponent()).setOpaque(false);

setUI(new BasicComboBoxUI() {

   @Override    
   public void paintCurrentValueBackground(
       Graphics g, Rectangle bounds, boolean hasFocus) {

   }
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM