Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Panel not repainting  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm doing some AWT stuff here.
I've got this frame with a couple of panels, a menu bar and a few buttons in the panels.
Now, the main panel is not displaying properly until I manually resize the frame (by dragging on the corner of it).
Is there a typical cause and solution for this problem?
(I know I'm not giving much to work with, but any suggestions?)
Thanks!
Drew
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the statement after adding all the components to the frame.
frame.pack();
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, the pack() method does work, and all the components within the frame display properly.
Unfortunately, it's don't really want to pack the frame.
I want to set the frame at a predefined size initially, say 400 x 400.
Is there another way to force the frame to repaint all its components?
I've tried stuff like validate(), and repaint(), but it doesn't seem to work.
Is there another procedure that will force a redraw of the components similar to pack() without the packing?
Also, if someone could explain what's really going on here I would be grateful!
Regards,
Drew
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a layoutmanager, try not using one, setting setLayout(null), and manually size your Panels inside your frame. Drawback of this is having to position every single component on the frame and panel.
But I don't like the restricions of LayoutManagers so I just don't use one.
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
frame.setSize(400,400);


 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I'm already using the setSize method.
Also, I don't want really want to use a null layout.
Are there any other ways I can force the layout manager to redisplay the frame?
For example, should I try some combination of invalidate() and validate() on the panel and/or frame ?
Thanks,
Drew
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people ..

Sorry for bringing up this old thread. I've been struggling with the same problem here using AWT.

So in brief :
- I'm using awt to create a grid/table component
- I extend the java.awt.Label and override the getPreferredSize() method to have control over it as the awt components doesnt have the setPreferredSize()
- I am currently using the flexible GridLayout available from javaworld tip as the layout
- I try to dynamically change the preferred sizes of the label, and it changes allright, but call to repaint doesnt do anything
- In fact i've tried the combinations of repaint, invalidate, validate, paint, overriding the validate to call paint, etc
- But when i resize the frame, the panel which uses the gridlayout, and which contains the label, redraws itself successfully. The label draws with it's current preferredSize.
- I've been searching inside the java.awt.Frame trying to find the code that call repaints when the frame is resized but couldnt find any

Please share your thoughts , haha

Regards,
Albert Kam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!