Solved

Title/Icon bar thickness on Frame

Posted on 2004-03-31
8
341 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

759 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