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?
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 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.
Joined: Apr 03, 2011
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.
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: