tags:

views:

259

answers:

3

I need to find a component's exact screen size in pixels when the main JFrame is resized.

I've tried several things and couldn't find an easy way to do it: it probably have missed something obvious.

BBBBB JFRAME BORDER BBB
BZZZZZZZZZZZZZZZZZZZZZB
BAAAAAAAAAAAAAAAAAAAAAB
BCC1................DDB
BCCC................DDB
BCCC................DDB
BCCC................DDB
BCCC................2DB
BEEEEEEEEEEEEEEEEEEEEEB
BBBBBBBBBBBBBBBBBBBBBBB

Resized, it could become this if, say, the user made the main JFrame shorter (vertically) and wider (horizontally):

 BBBBBB JFRAME BORDER BBBBBB
 BZZZZZZZZZZZZZZZZZZZZZZZZZB
 BAAAAAAAAAAAAAAAAAAAAAAAAAB
 BCC1....................DDB
 BCCC....................2DB
 BEEEEEEEEEEEEEEEEEEEEEEEEEB
 BBBBBBBBBBBBBBBBBBBBBBBBBBB

What I want is to find the exact size in pixels, on screen, of the rectangle area indicated by dots.

I'm adding a ComponentListener to get the resizing events, which works fine.

The issue I'm having is that calling getWidth()/getHeight() on a Component does apparently not return the component's screen size but the component's actual size (and you can have, for example, a JPanel wider than the component it is into).

Any hint most welcome.

+1  A: 

getWidth() and getHeight() returns the width and height of the component. I've never heard of any difference between "actual" width/height and "screen" width/height.

Some layout managers however, does not fill the panel with a component it contains, and some space between the component and the panels edge may not be occupied by the component. In that case I usually setBackground(Color.BLUE) on the underlying panel to see what's going on.

aioobe
@aioobe: you've never seen a JPanel who's content is cropped because the JFrame is smaller than the JPanel !?
Webinator
If that's the behavior you see, then it seems like your layout-manager is broken. Which layout manager are you using?
aioobe
@aioobe: IntelliJ IDEA's GridLayoutManager which is probably used for IntelliJ IDEA itself. I somehow have doubt it is broken but I'll try with a BorderLayout to see.
Webinator
@aioobe: can you explain me what happens when you put a component that has a minimum width of, say, 100, in a JFrame that is 50 pixels wide. What is a non-broken layout-manager supposed to do here?
Webinator
Well take GridLayout for instance, it will not respect the minimum size. I believe minimum/maximum/preferred size are seen as "hints" for the layout manager. Can't find a reference for it though.
aioobe
From [Suns Java Tutorial on layout managers](http://java.sun.com/docs/books/tutorial/uiswing/layout/problems.html)> If the component is not controlled by a layout manager, you can set its size by invoking the setSize or setBounds method on it. Otherwise, you need to provide size hints and then make sure you are using a layout manager that respects the size hints.
aioobe
@aiooble: oh I see... Still haven't fixed my issue but thanks a lot for the links and explanation, good read :)
Webinator
Okey, @Nate just provided the solution for you! I had no idea about that method!
aioobe
A: 

This is a very rubbish way but you can use a wrapper Panel, where you put your main panel into. Than you can call getWidth() and getHeight() from the wrapper panel.

Martijn Courteaux
@Martijn Courteaux: he he, I tried that too but in my case it didn't work: I still would have component(s) giving me a getWidth()/getHeight() that would *not* be the real w/h. However Nate's answer is right on spot: *getVisibleRect()* does **really** give back the real visible rect :)
Webinator
+6  A: 

You're looking for JComponent.getVisibleRect().

Nate
@Nate: thanks a lot, I *knew* I was missing something obvious. Solves my problem nicely and works fine with the layout manager I'm using etc. :)
Webinator