• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is this JLabel not showing up?  RSS feed

 
Devin Leeman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Just joined the Ranch in hopes that you all could help me with a simple GUI issue I am having.  Just starting to write the GUI for my program and can't get this label to show up on my panel as intended.  Any help would be greatly appreciated.



I just want it to show up at the top, aligned in the center, with the formatting I have applied.

-Devin
 
Devin Leeman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-Ooops, didn't realize that was just an ad to the "Programming Style Guide" and not someone responding.  Ignore this second post, ha.

I just parsed through that link and didn't see anything that would change the outcome of what I am trying to do.  Am I missing something?  I used practically the same exact code on another program I wrote and that worked, can't seem to pinpoint why this won't.  Any other advice?

Thanks in advance,
-Devin
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

without packing objects may not be in their proper location to be displayed.
 
Devin Leeman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That particular fix resizes the frame that I have made for the main menu and then fits the label into it.  I would prefer to keep the size I have set for the frame and just apply this at the top.  I will keep playing with it in hopes that something will click.  Anymore advice would be greatly appreciated, thanks.

-Devin
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to setVisible shows the state of the GUI up to that point.  Changes made after that won't be shown until some event.
Move the call to setVisible to after everything you want to see is in place.
 
Devin Leeman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, that's it.  Thank you so much everyone for chiming in.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran your original code and it works for me (?). I'm using Java 8 update 92 inside of Eclipse.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably just force of habit but I always call setVisible() after the GUI has been entirely configured.
 
Devin Leeman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, will start setting the frame to visible after everything is drawn on.

Furthermore, how can I make this specific cell to anchor at the top of the frame?  Right now it just places it in the center of the frame.  Still learning the GridBagLayout, this is sort of a trial by error program for me to get a decent grasp on the layout.

Thanks everyone, you've been very helpful thus far.  Welcoming community indeed!

-Devin
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran your original code and it works for me 
It might be a timing thing because the code is not being executed in the EDT.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
without packing objects may not be in their proper location to be displayed.
I can never get pack() to work; I think you have to call setPreferredSize() on (at least some of) the Components before pack() will give a sensible size to the whole display. As others have said, the real problem is calling setVisible too early. It isn't just force of habit.
I think you don't need to use preferred sizes if you use grid bag; use fill constraints instead. I suggest you find out about Horstmann's GBC class which makes grid bag easier to use.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problems is that you are adding components after the frame is made visible. To get that to work correctly

1) Add components and then make the frame visible (recommended) OR
2) Invoke revalidate() and repaint() in that order on the base container
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!