Title/Icon bar thickness on Frame

I have an app that positions a component (JButton say) and draws a Rectangle2D around it.
This using Swing and Java2D with absolute positioning:
rect = new Rectangle2D.Double(75,75,60,45);

Okay, works fine until I run the app on my laptop and the button and rectangle don't line up.

It seems that I use Large Fonts for my Win98se under Properties/Settings and Small Fonts for my laptop. Yes, has the same (not wanted) result if I change the settings to Small.

I believe that Swing positions 0,0 from the working area of the frame (for me a gray color) and Java2D starts at the top corner of the frame (above the blue icon/title bar). For Large Fonts that bar is thicker and so the button moves down.

What I need is a Swing version like JRectangle ( and JLine for that matter).

Anything like this exist or is there another fix?

Thank you, Squeeto
Who is Participating?
kiwi_alienConnect With a Mentor Commented:
Ok if I create my JFrame with size 200,200.

JFrame frame = new JFrame(200,200);

and see how big my contentPane is...

Rectangle rect = frame.getBounds();

And print it out I get something like...


So what happened to my 200,200?  I lost 4 pixels each side and from the bottom so looks like I lost 23 pixels with the title bar etc.

Mileage will vary depending on Font used in Titlebar obviously.  I figure from that you can fudge a little and bang your box in the right place every time.

Course I still agree with objects and figure using a border is a better solution...  (-;
don't use absolute positioning.
instead use setBorder() to paint your rectangle.
or do your paining on the frames content pane.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Don't set the size of the button - that really is just asking for trouble (Java is multi-platform and I'm sure you'd see weird stuff on a Mac, etc..)

Just let your button be sized by swing (with whatever constraints you would like)

Then draw your rectangle based on what the button thinks is its size is.  Just use myButton.getBounds() and render your Rectangle2D appropriately...  

If you want to know how big the title bar is and fudge appropriately then grab the content pane of the Frame and check its location.
SqueetoAuthor Commented:
For this application, I would prefer to stay with absolute location and size.

I never had this problem when I used Buttons, Labels only JButtons, JLabels etc.

Kiwi Alien,
>grab the content pane of the Frame and check its location
Can you give me a specific line of code?
That was a symptom of not enough proof-reading - should have been...

Rectangle rect = frame.getContentPane().getBounds();

Yep - it does work...
SqueetoAuthor Commented:
Kiwi Alien,
Yep - it does work...
For your 200x200 example, I got a height of 170 for Large Fonts and 175 for Small.  It is easy to adjust the positioning now.

BTW, I've had this question posted at Sun's Forum for 3 days now with maximum Duke dollars and have yet to get a response.  Good work and thanks.

Happy to oblige...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.