banner



How To Make Background Fully Transparent Java

Summary: How to make a Coffee JFrame transparent (translucent) on Mac Bone X.

A lot of people complain nearly a lot of things in regards to Coffee on Apple's Mac OS Ten platform, and okay, occasionally I'grand 1 of them, but a very cool affair you can do on Os X is to create translucent (transparent) frames and windows with Java.

Every bit an case of this, today I'm going to share some Coffee Swing source code where I create a translucent JFrame. To arrive a petty more interesting/existent, I've added a a JTextArea and JScrollPane to evidence you how it works, and how information technology looks.

Create a transparent JFrame with one magic line

Actually, creating a transparent JFrame on Mac Os Ten is no big deal, at least not as long as you're using the right software versions. The only line of code you lot really demand is shown here:

editorFrame.getRootPane().putClientProperty("Window.blastoff", new Float(0.8f));        

This special Window.alpha property -- which is Mac OS Ten specific -- lets you set the translucency level of your JFrame or JWindow. If y'all're not aircraft your product to other users on a variety of other systems where you demand to check the operating system and revision level, this is all you accept to practise.

Y'all can set this Window.blastoff property anywhere between 0.0 and 1.0, with the lowest values making your window almost invisible. I'll show the effect of different settings shortly.

My Coffee translucent JFrame source code

I've written an case Java/Swing application to demonstrate this JFrame transparency issue on Mac Bone X, and I'm sharing the source code hither. Most of the code is boilerplate Java Swing code, only hither'due south a quick clarification of information technology:

  1. I create a JFrame object named editorFrame.
  2. I create a JTextArea, place that in a JScrollPane, and place that in the middle panel of the JFrame's default BorderLayout.
  3. I set up the Window.blastoff setting, as shown above.
  4. I center the JFrame, and then brand it visible.

Given that introduction, here's my sample Java code:

package com.devdaily.swingtests.transparency;  import javax.swing.*; import coffee.awt.BorderLayout; import java.awt.Dimension;  /**  * Creates a translucent frame (jframe) on Mac Os X.  * @author alvin alexander, devdaily.com  */ public class MacTranslucentFrame {   public static void chief(String[] args)   {     new MacTranslucentFrame();   }    public MacTranslucentFrame()   {     SwingUtilities.invokeLater(new Runnable()     {       public void run()       {         JFrame editorFrame;         editorFrame = new JFrame("Java Mac OS 10 Translucency Demo");         editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);                  // this is what sets the transparency/translucency on Mac OS X         editorFrame.getRootPane().putClientProperty("Window.blastoff", new Float(0.8f));                  // create and add a curlicue pane and text area         JTextArea textArea = new JTextArea(5, 30);         JScrollPane scrollPane = new JScrollPane(textArea);         textArea.setText("Hello, world");         scrollPane.setPreferredSize(new Dimension(300, 185));         editorFrame.getContentPane().add(scrollPane, BorderLayout.Middle);          editorFrame.pack();         editorFrame.setLocationRelativeTo(null);         editorFrame.setVisible(true);       }     });   }  }        

Sample screenshots

Hither are a few sample screenshots of this application running. I ready the translucency at iii dissimilar levels for these shots: 0.8f, 0.6f, and 0.4f. The application is running in forepart of a blueish background, with a folder intentionally placed backside the JFrame.

Here's the translucency with a setting of 0.8f:

Here'south roughly the same screen shot with a translucency with a setting of 0.6f:

And finally hither'due south roughly the same screen shot with a translucency with a setting of 0.4f:

This is a very cool effect for Java Swing applications on the Mac platform. I've written my own custom editor that I utilize on the Mac, and this is one of those nice effects that gives an application a piddling extra "something" that customers appreciate.

How To Make Background Fully Transparent Java,

Source: https://alvinalexander.com/blog/post/jfc-swing/how-create-transparent-translucent-java-frame-jframe-mac/

Posted by: barteltdoccap.blogspot.com

0 Response to "How To Make Background Fully Transparent Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel