user1610362
user1610362

Reputation: 647

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

}

}

Upvotes: 2

Views: 5611

Answers (4)

Raul
Raul

Reputation: 483

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

Upvotes: 1

user2960722
user2960722

Reputation: 1

try this.

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

setUI(new BasicComboBoxUI() {

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

   }
});

Upvotes: -1

Nick Rippe
Nick Rippe

Reputation: 6465

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

Upvotes: 2

Vuk Vasić
Vuk Vasić

Reputation: 1408

You need to preset this few things

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

Upvotes: -1

Related Questions