Download NetBeans!

20080531 Saturday May 31, 2008

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.

May 31 2008, 06:25:50 AM PDT Permalink