m121212
m121212

Reputation: 151

Recreate Windows Look-and-Feel border of JFrame or JDialog?

I need to recreate the translucent border of JFrame or JDialog that appears when setting the windows look and feel in swing.

I need it because Windows LaF does not let you access the title bar (on the border). In fact, I need to apply a MouseAdapter to the JDialog that gets notified when it is dragged/pressed/released. In windows laf, as you cannot get access to the bar component, you can only apply a ComponentListener which gives you notification only when moving (so you don't capture anything when the user has grabbed it but hasn't moved yet, or either when the user "releases" it).

Therefore, I decided to go with undecorated dialogs and apply the listeners to my custom bar. However I want the custom dialog looks exactly as in windows laf (it means I need to recreate the border).

I'm not very experienced in Graphics2D to override the paintBorder() method, so I'm asking for your help.

Has anyone ever faced this problem and has a tested solution?

Upvotes: 0

Views: 321

Answers (1)

mr.celo
mr.celo

Reputation: 585

As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, you can add translucent and shaped windows to your Swing applications.

This means that you can have your JFrame emulate a native window with the rounded corners and transparency.

In your case, your approach would be in the JFrame level instead of the border level because the border is painted on top of the JFrame (or JDialog, for that matter). Therefore, if the JFrame is not already rounded, for instance, the paintBorder() method will still be painting on top of a rectangular area of the screen.

Check this tutorial from Oracle covering shaped and translucent JFrame.

Upvotes: 1

Related Questions