5

Yes, this question is everywhere. But all of the (working) solutions use the AWTUtilities toolkit, which is restricted.

So. I just want to control the opacity of my window. No need to shape or undecorate. Just a transparent contentPane (easy), and a transparent JFrame background (ridiculously difficult).

I could have sworn I got the right combination of stuff yesterday, but now I can't seem to reproduce it. Also there are some solutions out there without using AWTUtilities, but they don't work...does someone have a good solution?

An example of my failing code:

public static void main(String[] args) {
    JFrame test = new JFrame();
    test.setSize(400, 400);
    test.setLocationRelativeTo(null);
    test.setUndecorated(true);
    test.setBackground(new Color(0, 0, 0, 0));
    test.getContentPane().setBackground(new Color(0,0,0,0));
    test.setVisible(true);
}

but this just makes a white square. Close but no cigar. I've also tried overriding the paint method, and someone was saying something about throwing out the alpha channel, but that made it black (of course). So...Stack Overflow it is.

If there's a dupe that answers this exactly, please point me to it and I'll delete right away.

Update Per requests in comments, here's a history of how I arrived here:

The first link everyone arrives at is how to create translucent and shaped windows. This has several lines in there similar to "...the frame translucent with a 75% level of opacity." So...looks like they're defined the same, at least in that article. Unfortunately they use the library.

A couple links that I could chase down:

A non-working "working" solution is reported at http://www.java-gaming.org/index.php?topic=24635.0 which shows some hope but I couldn't get it working.

http://techgearup.wordpress.com/2008/09/19/transparent-jframe-background/ is a hackish way using screenshots and is duplicated in a few other places.

Ben
  • 54,723
  • 49
  • 178
  • 224
  • 3
    Can you use Java 7? Could you link to some of the working solutions so that we see what you have tried? – Joachim Sauer Oct 04 '11 at 13:10
  • 1
    Please don't [confound](http://stackoverflow.com/questions/3517722/java-transparent-jscrollpane/3518047#3518047) _opacity_ and _transparency_. – trashgod Oct 04 '11 at 13:15
  • Agreed, this is one of the new features on Java 7: http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html – Shivan Dragon Oct 04 '11 at 13:15
  • 1
    @trashgod: he's not, you are, opacity and transluceny refer to the same thing. From the Java documentation : "boolean isWindowOpaque(Window window) Returns whether the window is opaque or translucent" – Shivan Dragon Oct 04 '11 at 13:17
  • What platform? Does [this](http://stackoverflow.com/questions/2163544/re-paint-problem-on-translucent-frame-panel-component/2166500#2166500) work? As the term is overloaded, see also [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/#swing). – trashgod Oct 04 '11 at 13:27
  • @Joachim Sauer - not sure all of my users have Java 7 (yet) so am forced to rule it out at this point. – Ben Oct 04 '11 at 13:49
  • @trashgod - I'm trying to control the opacity, per the description in the other post. Ultimate goal is an overlay, on...all? platforms. – Ben Oct 04 '11 at 13:50
  • Good luck with this. I ran into the same problem and ultimately I went with AWTUtilities. – Erick Robertson Oct 04 '11 at 14:07
  • @Erick Robertson - looks like that's a well-traveled path. – Ben Oct 06 '11 at 00:45
  • Why did you just now delete your question about an opaque JButton leaving residue? – Hovercraft Full Of Eels Oct 07 '11 at 00:52

2 Answers2

1

You can't do this without using Java v.6u10+ or third-party libraries that call native code. In Swing Frames, Dialogs, and Windows are considered 'top-level components' and are rendered in the underlying windowing system. Before Java v6u10, all top-level components had a default, opaque light-grayish background, and there was no way to change this from Java code.

Nate
  • 16,748
  • 5
  • 45
  • 59
  • Apparently Java 6u10 has been out since October 2008, which is long enough for my users to have it. How can this be accomplished in releases after Java 6u10? – Ben Oct 05 '11 at 00:33
  • In 6u10 - you have to use AWTUtilities, after 7 they added methods to Window and some other classes. http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#6u10 (I realize you've set AWTUtilities is "restricted", but that's basically the only way Java provides for you to do this.) – Nate Oct 05 '11 at 14:17
  • OK thanks for the help. I'm working on an open-source project and I'm not sure the attitude toward unrestricting files. But that's a different question :) – Ben Oct 06 '11 at 00:44
0

Sigh I came across the same problem, and after a lot of time working on it, I found a way to get a properly set transparent screen on my computer. However, a lot of things stop working as soon as you set the frame transparent. If you use JTextArea declaration anywhere, it immediately stop working. It won't even throw an error!

So here's the code, hope this is of some use. Plz reply if you make a working JScrollPane with it :)

public class gui {
    public frame f;
    public String name = "Transparent?"; //name your frame *
    public JButton exit = new JButton("Exit");
    public gui(){ //non-static context
        f = new  JFrame(name);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //*
        f.setLocationRelativeTo(null);
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        //maximizes window. if you dont want it, f.setSize(int, int)
        f.setUndecorated(true); //necessary for setOpacity
        f.setOpacity(.7f); //achieve trasparancy
        f.setVisible(true); //show window
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.exit(0);
            }
        });
        f.add(exit, BorderLayout.CENTER); 
        //since X is not available in the window, make a button to exit
    }

    public static void main(){
        new gui();
    }
}
//* Note: since the default Window manager is set off with setUndecorated(true),
// the name and buttons won't really show. 
nkazuko
  • 1
  • 1
  • Oh I forgot to mention, JTextField still works. The only thing that doesn't work so far (the things I was using) is JTextArea and JScrollPane I haven't yet tested borders but it isn't the main concern – nkazuko Feb 28 '12 at 00:24