Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting JPanel size

 
Lucas Quick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lucas Quick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be done. You can't know the size of a component until it's been materialized on the screen.
 
Lucas Quick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I did a workaround that using the componentListener to watch for window resizing. Still, thanks for quick replies.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic