This discussion is archived
3 Replies Latest reply: May 18, 2013 12:09 PM by 998435 RSS

Transparent JFileChooser

998435 Newbie
Currently Being Moderated
Hi!

I have yet another "how can I make this thing transparent" question, this time for the JFileChooser. I've written a function to recursively iterate through its components and these are the ones I can find:
class javax.swing.JPanel
class javax.swing.JPanel
class javax.swing.JButton
class javax.swing.Box$Filler
class javax.swing.JButton
class javax.swing.Box$Filler
class javax.swing.JButton
class javax.swing.Box$Filler
class javax.swing.JToggleButton
class javax.swing.JToggleButton
class javax.swing.JLabel
class javax.swing.plaf.metal.MetalFileChooserUI$1
class javax.swing.JPanel
class sun.swing.FilePane
class javax.swing.JPanel
class javax.swing.JPanel
class javax.swing.plaf.metal.MetalFileChooserUI$AlignedLabel
class javax.swing.plaf.metal.MetalFileChooserUI$3
class javax.swing.Box$Filler
class javax.swing.JPanel
class javax.swing.plaf.metal.MetalFileChooserUI$AlignedLabel
class javax.swing.JComboBox
class javax.swing.JPanel
class javax.swing.JButton
class javax.swing.JButton
Running setOpaque(false) on all instances of JComponent makes everything but the list of files transparent. From what I can see it's some kind of JList but it doesn't appear in the list above. Is there a way to make this last part transparent as well? Can I somehow run setOpaque(false) on it too?
  • 1. Re: Transparent JFileChooser
    thomas.behr Newbie
    Currently Being Moderated
    Have you tried subclassing JFileChooser, overwriting its createDialog method, and setting the created dialog's opacity (see setOpacity), instead of calling setOpaque(false) for all components that make up JFileChooser? See http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html for more information on non-opaque windows.
  • 2. Re: Transparent JFileChooser
    Maxideon Explorer
    Currently Being Moderated
    Did the combo box's become transparent on your machine by simply setting the opacity? They don't on mine. The cell renderer inherits its background color from the combo box and its opacity never gets set false since its not in a hierarchy until its about to be displayed. This is probably the same thing that's happening with the lists.
  • 3. Re: Transparent JFileChooser
    998435 Newbie
    Currently Being Moderated
    Thanks for your replies!

    I realise now I probably should have put some more effort into this before posting. I've created an example that'll hopefully make things clearer.

    Maxideon: You're right, ComboBox doesn't become transparent, it did in my code because I'd set a custom ComboBoxUI.

    It's only the list of files I'm interested in making transparent right now, I've managed to get all of the other components transparent already. Here's the code:

    FileChooserTest.java:
    import java.awt.*;
    import javax.swing.*;
    
    public class FileChooserTest extends JFrame {
      String background = "bkg.jpg";
    
      public FileChooserTest() {
        ImageIcon imageIcon = new ImageIcon(background);
        setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());
    
        JPanel panel = new JPanel() {
          public void paintComponent(Graphics g) {
            g.drawImage(new ImageIcon(background).getImage(), 0, 0, null);
          } 
        };
        panel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
    
        UIManager.put("FileChooserUI", MyFileChooserUI.class.getName());
    
        panel.add(new JFileChooser());
        getContentPane().add(panel);
      }
    
      public static void main(String[] args) {
        new FileChooserTest().setVisible(true);
      }
    }
    MyFileChooserUI.java
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.ComponentUI;
    import javax.swing.plaf.metal.MetalFileChooserUI;
    
    public class MyFileChooserUI extends MetalFileChooserUI {
    
      public static ComponentUI createUI(JComponent c) {
        return new MyFileChooserUI((JFileChooser) c);
      }
    
      private MyFileChooserUI(JFileChooser c) {
        super(c);
      }
    
      public void installComponents(JFileChooser fc) {
        super.installComponents(fc);
        customize(fc);
      }
    
      /* Set opacity, call recursively */
      private void customize(Container c) {
        int len = c.getComponentCount();
        for(int i = 0; i < len; i++) {
          Component comp = c.getComponent(i);
    
          if(comp instanceof JComponent) {
            ((JComponent)comp).setOpaque(false);
          }
    
          if(comp instanceof Container) {
            customize((Container) comp);
          }
        }
      }
    }
    thomas.behr: Does your solution work for a JFileChooser added to a JPanel or only when it's a separate Window?

    Is there a way to create a custom ListUI that'll make the list in the JFileChooser transparent?

    Once again, thanks guys and sorry for not creating an example from the start.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points