7

Related to this question: Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?

with jdk1.6.0_26 I seem to be able to apply translucency to a JFrame, but not so with jre7:

NativeException: java.awt.IllegalComponentStateException: The frame is decorated

ex (jruby scripting java, works jdk1.6 not with jdk7 though):


require 'java'

class MouseDraw

  def self.go
    java_import 'javax.swing.JFrame'
    java_import 'com.sun.awt.AWTUtilities'

    f = JFrame.new
    AWTUtilities.set_window_opacity(f, 0.5)
    f.set_size 200,200
    f.show
  end

end
MouseDraw.go

So my question is "is it possible to have a translucent title bar in jdk7" (I would like a transparent window I'm creating to be draggable/resizable)

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388

2 Answers2

8

Java 7 introduced Window.setOpacity() to do what the unofficial class AWTUtilities did in Java 6.

Unfortunately it's documented not to work with decorated windows:

The following conditions must be met in order to set the opacity value less than 1.0f:

(Emphasis mine)

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • @kleopatra: but that's what you signed up for when you used unofficial API: The `AWTUtilities` are not defined in Java SE and the new method doesn't break compatibility with any previously defined public method. – Joachim Sauer Sep 09 '11 at 09:44
  • 1
    technically, you'r right, also technically, I didn't: instead used the AWTUtilitiesWrapper which was a half-official preview of the to-be functionality (and advertised as rather safe ;-) – kleopatra Sep 09 '11 at 09:56
  • 1
    judging from the tutorial being outdated, this looks (wild guess) like a rather late emergency brake for some wild or not so wild side-effect in decorated windows (bye bye fading out ;-) – kleopatra Sep 09 '11 at 10:00
  • So I have a java 7 program that uses decorated windows that are translucent (and it works on ubuntu). Then running the same program on windows doesn't work. explain that one. – Joelle Boulet Jul 23 '15 at 16:13
  • @JoelBoulet: I don't really know what you expect. I think you should post this as a question with a bit more information to it. – Joachim Sauer Jul 23 '15 at 16:15
3

Fascinatingly, if you add

JFrame.setDefaultLookAndFeelDecorated(true);

Then it allows you to have a draggable, with title bar JFrame (it just uses the ugly java look and feel).

ref: Pass mouse events to applications behind from a Java UI

Now that is weird.

Also note that you can "fake" the old behavior using per pixel transparency, see http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

so you can have the old behavior in both java 6 and 7, but you'd have to special case the code around it and do it one way for one, one way for the other, possibly...

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388