Solved

Title/Icon bar thickness on Frame

Posted on 2004-03-31
8
342 Views
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:
button.setBounds(80,60,45,30);
...
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
0
Comment
Question by:Squeeto
  • 4
  • 2
  • 2
8 Comments
 
LVL 92

Expert Comment

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

Expert Comment

by:objects
ID: 10728338
or do your paining on the frames content pane.
0
 
LVL 1

Expert Comment

by:kiwi_alien
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.
0
 

Author Comment

by:Squeeto
ID: 10729320
Objects,
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?
S.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
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...

java.awt.Rectangle[x=0,y=0,width=192,height=173]

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...  (-;
0
 
LVL 1

Expert Comment

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

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

Yep - it does work...
0
 

Author Comment

by:Squeeto
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.

Squeeto
0
 
LVL 1

Expert Comment

by:kiwi_alien
ID: 10729692
Happy to oblige...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 126
allswap challenge 6 79
java  and programming certification ? 4 86
couple of eclipse 5 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now