• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is this JLabel not showing up?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, that's it.  Thank you so much everyone for chiming in.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic