Title/Icon bar thickness on Frame

Posted on 2004-03-31
Last Modified: 2008-03-04
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
Question by:Squeeto
  • 4
  • 2
  • 2
LVL 92

Expert Comment

ID: 10728324
don't use absolute positioning.
instead use setBorder() to paint your rectangle.
LVL 92

Expert Comment

ID: 10728338
or do your paining on the frames content pane.

Expert Comment

ID: 10728405
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10729320
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?

Accepted Solution

kiwi_alien earned 250 total points
ID: 10729406
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...  (-;

Expert Comment

ID: 10729411
That was a symptom of not enough proof-reading - should have been...

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

Yep - it does work...

Author Comment

ID: 10729683
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.


Expert Comment

ID: 10729692
Happy to oblige...

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 43
eclipse buid path vs tomcat lib path 10 37
ejb on wildfly 5 45
Coding for the first time 9 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question