Reshaping NetBeans

Reading How to Create Translucent and Shaped Windows, I discovered a link at the end to a NetBeans project. That's pretty cool. And I've been using JDK 6 Update 10 Beta for quite a while now, so there was nothing special I needed to do to get set up. So I narrowed the code down to its absolute basics. I started by creating a new JFrame using the JFrame Form template in the IDE. Importantly, to create a round JFrame, you need to set its "undecorated" property to false (in Matisse GUI Builder that means just a click in a checkbox in the Properties sheet). Then define the main method like this:

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        ComponentListener shapeListener = null;
        @Override
        public void run() {
            final JFrame frame = new NewJFrame();
            frame.addComponentListener(shapeListener = new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent evt) {
                    Shape shape = null;
                    shape = new Ellipse2D.Float(0, 0, frame.getWidth(), frame.getHeight());
                    AWTUtilities.setWindowShape(frame, shape);
                }
            });
            frame.setVisible(true);
        }
    });
}

Then run it and you have an ellipse instead of your boring old pointy cornered JFrame. Speaking of which, replace the line that defines the shape above with this one and you'll have a less radical result, i.e., not an ellipse, but a rounded rectangle:

shape = new RoundRectangle2D.Float(0, 0, frame.getWidth(), frame.getHeight(), 30, 30);

It's kind of funny when you're experimenting with this, because your JFrame is normal in design mode, while being transformed at deployment:

However, that's only the start. Since it is possible to grab the main window of the NetBeans Platform and cast it to a JFrame, you can do fun (though probably pointless) things like this:

The above is made possible via this Installer class in a NetBeans module which, when plugged into the NetBeans Platform, produces the results above:

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        SwingUtilities.invokeLater(new Runnable() {
            private ComponentListener shapeListener = null;
            public void run() {
                final JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
                frame.setUndecorated(true);
                frame.addComponentListener(shapeListener = new ComponentAdapter() {
                    @Override
                    public void componentShown(ComponentEvent evt) {
                        Shape shape = null;
                        shape = new Ellipse2D.Float(0, 0, frame.getWidth(), frame.getHeight());
                        AWTUtilities.setWindowShape(frame, shape);
                    }
                });
            }
        });
    }
}

An error is thrown because the "setUndecorated" is called at the wrong time. Not sure when to call it to fix this. Still, pretty cool to see the above results.

Other great articles on this topic are this one and this one by Kirill.

Comments:

Interesting.
Seems you have to call dispose before setUndecorated and the make the frame visible again in the netbeans version:
http://forum.java.sun.com/thread.jspa?threadID=235247&messageID=2701575

Posted by Ramon on May 31, 2008 at 11:41 PM PDT #

Thanks. Works perfectly now!

Posted by Geertjan on May 31, 2008 at 11:49 PM PDT #

check netbeans module core.nativeaccess, it'll let you do the same even without jdk6u10

Posted by Stan on June 01, 2008 at 09:26 PM PDT #

Fails because there is no such class as AWTUtilities.

Posted by qwentin on September 30, 2008 at 05:28 PM PDT #

Fails because you're not using JDK 6.

Posted by Geertjan on October 01, 2008 at 02:44 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
24
25
26
27
28
29
30
   
       
Today