X

Geertjan's Blog

  • May 31, 2008

Reshaping NetBeans

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 7 )
  • Ramon Sunday, June 1, 2008

    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


  • Geertjan Sunday, June 1, 2008

    Thanks. Works perfectly now!


  • Stan Monday, June 2, 2008

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


  • qwentin Wednesday, October 1, 2008

    Fails because there is no such class as AWTUtilities.


  • Geertjan Wednesday, October 1, 2008

    Fails because you're not using JDK 6.


  • guest Monday, March 21, 2016

    for those of you looking for the contents of the linked thread :

    _frame.dispose(); // This is the key guy, you need to dispose the frame first.

    _frame.setUndecorated(true);

    _frame.setResizable(false);

    _frame.setVisible(true);

    _frame.toFront();


  • Jay.Xac Monday, March 21, 2016

    The link is dead, here is the significant part.

    _frame.dispose(); // key part is to dispose the frame.

    _frame.setUndecorated(true);

    _frame.setResizable(false);

    _frame.setVisible(true);

    _frame.toFront();


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services