Click here to Skip to main content
13,596,314 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 10 Oct 2010
Licenced Apache

Arbitrary Shaped Transparent JFrame in Java Swing

, 11 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Create a Shape for a custom JFrame and apply a transparency effect (0 to 1 alpha value) on entire window, on Windows Platform


The aim of this work is to create this cool window:


We have a copy of JFrame behind, but with cool transparency effect and a rounded shape form. The difficulty is to recreate the window header, with title and button for window_state. But this article can clarify any doubts.

Graphic Package

The UML representation of simple package for our purpose is:


RoundedWindow is the object shown in previews screenshot. The basic component of package is ShapedFrame, this is an inheritance of JFrame with the ability to modify its shape (from simple rect shape of every window, even to a circle form) and can apply a transparency effect , on platforms where it is possible.

The JAVA utility to do that is AWTUtilities, but this is not an official release so I create a Wrapper for this AWTUtilities with a maximum level of abstraction so that if AWTUtilities will be changed to another package, you have only to modify AWTUtilitiesWrapper references and not all occurrences in the source code.

Since ShapedFrame is the basic inheritance, we have to create an extension like "SHAPEX"Frame, where in our case is RoundedRectFrame. This object extends ShapedFrame able to set a transparency and a shape form, and just have an alpha value and a basic shape form management. See below.

Finally there is an utility object: VisualDragHandle. What is that??? First, I say that a custom shape JFrame has to be "indecorated", so it does not have a handle for dragging all around the screen. So this is an object that has the ability to drag parent window when we click with mouse on itself. The only "must" is that we have to add it directly to contentPane of JFrame.

Now follows a simple explanation of each class.


public abstract class ShapedFrame extends JFrame {

 protected Shape shape;
 protected float alpha = 1f;
 public ShapedFrame() {
        //If is DECORATED effects are disabled


 private void setListenersForEffects() {
        //It is important to upadate visual effect on form resize.
        addComponentListener(new ComponentAdapter() {

            public void componentResized(ComponentEvent evt) {
 public void updateFrameEffects() {
        try {
            AWTUtilitiesWrapper.setWindowShape(this, shape);
            if (shape != null) {
                AWTUtilitiesWrapper.setWindowOpacity(this, alpha);
        } catch (Exception ex) {
            Logger.getLogger(ShapedFrame.class.getName()).log(Level.SEVERE, null, ex);

This is not the entire class but only the most important methods. There is the base constructor for setting "UNDECORATE" state of JFrame and for adding Listeners. updateFrameEffects is the core of advanced graphic of this package: it invokes two important methods of AWTUtilities for transparency and shaping. This method has to be called everytime when JFrame will be resized. setListenersForEffects links previous method to resize event.

The only properties are just alpha and shape.


Whereas the previous class is abstract (because every shape type has a different behaviour), we have to implement a real shape class like RoundedRectFrame.

public class RoundedRectFrame extends ShapedFrame {

    protected Dimension arcSize = new Dimension(50, 50);

    public RoundedRectFrame() {
        super(300, 200);

    public final void setShapeToDefault() {
        shape = new RoundRectangle2D.Double
	(0d, 0d, getWidth(), getHeight(), arcSize.width, arcSize.height);

    public void updateShape() {

Omitting getters and setters, this is the entire class. It is very simple because it has only to explain to father abstract class the behaviour of a RoundedRect shape when we want to draw it, resize it, and so on... and then? It only resizes the base RoundedRect by width and height of source JFrame. The peculiarity is to declare updateShape abstract in parent class; this method will be invoked every time JFrame updates itself; so overriding this method we will be sure that displayed shape is what we want.


This class is the end of workflow. The class is more comlex so I can't display all the source code.

public class RoundedWindow extends RoundedRectFrame {

    private JLabel titleLabel;
    private JPanel container;

    public RoundedWindow(JPanel defaultPanel) {
        super.setLayout(new BorderLayout());
        container = defaultPanel;
        setTitle("Rounded Window");

    private void addHeader() {
        titleLabel = new JLabel(getTitle());

        VisualDragHandle dragHandle = new VisualDragHandle(this);


        super.add(dragHandle, BorderLayout.NORTH);
        super.add(container, BorderLayout.CENTER);


This is only the base of the class. The default constructor gets as parameter a JPanel because it creates a custom JFrame where contentPane is not the effective pane where we work on. As you can see in the first screenshot, the RoundedWindow is made of a DarkGray header with title, and a JPanel with image as container. That is a JPanel I mean. Infact the rest of class is an override of JFrame methods for component management. An example is:

    public Component add(Component comp) {
        if (container != null) {
            return container.add(comp);
        return super.add(comp);

Why? Some users have been understood: after we have added a custom JPanel as container, when we will add a component to JFrame we want that component will be added to JPanel instead. Write comments if you don't understand.

addHeader method is the creation of header. Basically, we create a VisualDragHandle as seen before, and we add to this Handle, that is a simple JPanel, several elements like title and two object for Iconify and Close. The objects are retrieved by two methods: getIconifyComponent and getCloseComponent, simple methods that create two custom components, in this case two simple JLable with relative events, and return them to addHeader method for adding into header.


In the main class of source code, there is that core code:

CustomPanel panel = new CustomPanel();
CustomPanel panel2 = new CustomPanel();

//Standard JFrame creation
JFrame old = new JFrame();
old.setSize(700, 500);
old.setLayout(new BorderLayout());
old.add(panel2, BorderLayout.CENTER);

//New Cool Frame cration
final RoundedWindow roundedFrame = new RoundedWindow(panel);
roundedFrame.setSize(700, 500);

CustomPanel is a simple panel (with Dr.House photo & data: ), I have created this with netbeans so you ignore code. The peculiarity is the inheritance from ImagePanel that is a simple JPanel with image background, see article on DraggableImageComponent that is nearly the same thing. The inheritance is made because I want a cool glass background for my window.

The main code then creates with this custom component two frames: a simple JFrame and a RoundedWindow frame. You can see the visual differences among two JFrames in the first screenshot.


You now have a reusable component like RoundedWindow to create your simple small windows with transparency effects and rounded shape. Although you can extend ShapedFrame for your custom shapes like : Ovals, Circles, or Polyline; simply follow the RoundedRectFrame way. If you want (..with a simple vote!!! : ) I can post my work about several Shapes for JFrames for create Widget like translucent Clock or Calendar and so on...


Now I have tested this work on:

  • Windows Vista
  • Windows 7

The build release of JRE is jre1.6.0_17 and currently dosen't work on Linux gnome desktop. I haven't tried it on KDE. So write to me if you try.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Engineer n
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCreating custom shape Pin
siliyas3-Oct-12 22:32
membersiliyas3-Oct-12 22:32 
AnswerRe: Creating custom shape Pin
b4rc0ll03-Oct-12 22:59
groupb4rc0ll03-Oct-12 22:59 
QuestionRe: Creating custom shape Pin
siliyas3-Oct-12 23:22
membersiliyas3-Oct-12 23:22 
AnswerRe: Creating custom shape Pin
b4rc0ll03-Oct-12 23:24
groupb4rc0ll03-Oct-12 23:24 
GeneralRe: Creating custom shape Pin
siliyas7-Oct-12 5:48
membersiliyas7-Oct-12 5:48 
Questionzip file problem Pin
javarajch3-Nov-10 23:02
memberjavarajch3-Nov-10 23:02 
AnswerRe: zip file problem Pin
b4rc0ll03-Nov-10 23:05
groupb4rc0ll03-Nov-10 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180621.3 | Last Updated 11 Oct 2010
Article Copyright 2010 by b4rc0ll0
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid