Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make a JFrame transparent with solid border

 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am wondering how to make the content pane of a JFrame transparent, but not its border. The border is still opaque and solid.

Does anyone have ideas?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you try getcontentPane().setOpaque(false);?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote:What happens if you try getcontentPane().setOpaque(false);?
Container hans’t got a setOpaque method, so you’d get a compiler error.
There is such a method in JComponent. What happens if you set the content pane to a JComponent (eg JPanel) and call setOpaque(false) on that panel.
There appears to be a setOpacity(float) method in the AWT counterparts, too.
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said above, just create a JPanel instance and set it as the content pane, then call setOpaque(false) on that instance something like :
JPanel content = new JPanel();
yourjframe.setContentPane(content);
content.setOpaque(false);

but let me ask something, whats the point of making the content pane transparent without the jframe being transparent ?

Edit : if you really want to make the whole frame (including jframes, jpanels, etc.) see through, i can tell how to, just decide and reply. Even if you want solid borders, we can cook it together (trust me).
Well that was my $0.02 ;)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kocha Sapam wrote: . . . Well that was my $0.02 ;)
We usually charge a lot more than that for private work

And welcome to the Ranch
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that was hillarius, thanks ;)
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kocha Sapam wrote:As said above, just create a JPanel instance and set it as the content pane, then call setOpaque(false) on that instance something like :
JPanel content = new JPanel();
yourjframe.setContentPane(content);
content.setOpaque(false);


I just tried that, the whole frame is still opaque.


Kocha Sapam wrote:but let me ask something, whats the point of making the content pane transparent without the jframe being transparent ?

Well, the point is I want to create a resizable transparent window with only the border painted. This is a part of a screen recorder application.

Kocha Sapam wrote:
Edit : if you really want to make the whole frame (including jframes, jpanels, etc.) see through, i can tell how to, just decide and reply. Even if you want solid borders, we can cook it together (trust me).
Well that was my $0.02 ;)


Thanks, I already got it cooked
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^You mean your problem is solved ? If not, please specify your jdk version (like JDK6u14 above or JDK7 above, the solution differs on the version). Also, this solution *may not* work on linux.

Regards - Kocha
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kocha Sapam wrote:^You mean your problem is solved ? If not, please specify your jdk version (like JDK6u14 above or JDK7 above, the solution differs on the version). Also, this solution *may not* work on linux.

Regards - Kocha


Yes, I am using AWTUtilities to set the frame transparent, then I draw solid border on the glass pane. It works from Java 1.6 and above.
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's cool, exactly what I expected. But be careful, there is no com.sun.awt.AWTUtilities class as of JDK7, they have different classes in the java.awt package (i forgot the classnames, hope you already know them :P)

Ahhh! It's in the Window class, you can call setOpaque(false) on any of its decendents like jframe etc. So you'd simply call yourJFrame.setOpacity(0.0f) instead of AWTUtilities.setWindowOpaque(yourJFrame, false)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic