2

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

}

}
user1610362
  • 637
  • 2
  • 9
  • 26
  • can you show us where is problem on your side, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about JFrame and JComboBox – mKorbel Dec 18 '12 at 18:30
  • check this [post](http://www.pushing-pixels.org/2008/02/27/translucent-and-shaped-windows-in-core-java.html) out. – bonCodigo Dec 18 '12 at 18:31
  • 1
    @bonCodigo - The post you mention is about creating shaped and translucent windows. Don't think it will work for the JComboBox. – sreejit Dec 18 '12 at 18:42
  • @user1776749 I forgot to add 'check this post to get an idea'. And waiting to see an answer! – bonCodigo Dec 18 '12 at 19:13
  • The post you mention is about creating shaped and translucent windows. Don't think it will work for the JComboBox. – user1610362 Dec 18 '12 at 19:15
  • @Rohit Jain can throw some light here mate? :-) – bonCodigo Dec 18 '12 at 20:26

4 Answers4

2

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;
    }});
Nick Rippe
  • 6,465
  • 14
  • 30
  • In order to understand why the setOpaque trick wouldn't work for the Nimbus L&F, see this: http://stackoverflow.com/questions/2451990/setopaquetrue-false-java – lbalazscs Dec 18 '12 at 21:36
  • How do i remove border? I tried comboBox.setBorder(null); and comboBox.setBorder(new EmptyBorder(0, 0, 0, 0)); but it doesnt work. – user1610362 Dec 18 '12 at 22:43
  • That solution doesnt work with windows style JComboBox, UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());! – user1610362 Dec 18 '12 at 22:53
  • @user1610362 Yeah - a different L&F will change the way this works (since you're trying to edit the L&F). Do you need it to work in both Metal and Windows? Or just windows? Modifying your question to your specifics will help... – Nick Rippe Dec 18 '12 at 23:11
  • Only on windows and i need to remove borders. Also only ComboBox's text field not the popup. – user1610362 Dec 18 '12 at 23:40
1
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

Raul
  • 465
  • 5
  • 16
-1

You need to preset this few things

jcombo.setOpaque(false);
jcombo.setContentAreaFilled(false);
jcombo.setBorderPainted(false);
Vuk Vasić
  • 1,398
  • 10
  • 27
-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) {

   }
});
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • The `setUI` method just takes away the border and alters the LAF - for me at least. – Nash Sep 06 '18 at 13:49