aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Getting JPanel size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting JPanel size" Watch "Getting JPanel size" New topic
Author

Getting JPanel size

Lucas Quick
Greenhorn

Joined: Apr 03, 2011
Posts: 4
Hello everybody,

I have a problem with getting the correct size of the JPanel using getWidth() and getHeight() methods. The program is supposed to plot the function in the rectangle and should also have some text fields and a button. The problem is that the window has to be resizable and for that i need to use abovementioned methods which in my case return 0. I did some internet digging and I found out that reason for that might be either not using the layout manager (which I`m not supposed to use) or the fact that the panel is not fitted to the frame yet. Here`s the code:


How can I get proper width and height values to use them in the DrawWndPane constructor and PaintComponent function?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

If I replace wd and hg in that last line with "getWidth() / 2" and "getHeight() / 2" respectively, I get a rectangle that resizes as I resize the window. So what's not working for you?
I added the "/ 2" to make sure the rectangle doesn't fall off the screen.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lucas Quick
Greenhorn

Joined: Apr 03, 2011
Posts: 4
Oh, so that`s actually working. I confused it with a problem from another project. Anyway, the problem with constructor still remains, I`d appreciate any help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Lucas Quick wrote:Anyway, the problem with constructor still remains, I`d appreciate any help.


I'm sorry, I read the whole thread three times and I don't see anything about constructors and problems therewith.
Lucas Quick
Greenhorn

Joined: Apr 03, 2011
Posts: 4
The problem is that when I use getHeight() in the DrawWndPane constructor to determine the position of button and text fields the returned value is 0, so effectively it creates the button above the panel. I`d like the method to return the real height of the panel.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Can't be done. You can't know the size of a component until it's been materialized on the screen.
Lucas Quick
Greenhorn

Joined: Apr 03, 2011
Posts: 4
Well, I did a workaround that using the componentListener to watch for window resizing. Still, thanks for quick replies.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

This is what layout managers are for - to reposition the child components as necessary. (Well ok, also to determine the preferred size of your container.) If you can't find any layout manager that does what you want you can write one yourself. A quick attempt, as an inner class of DrawWndPane:
 
jQuery in Action, 2nd edition
 
subject: Getting JPanel size