Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't see my Panels!!! Aaarrghhhhh....

 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't this work? Or, rather, it works if I keep the label a Label, not a JLabel. When I change it to a JLabel, it will compile but I see nothing. Is there some rule about the order of panels or something? I must be inadvertantly covering up the panes I need or something.
 
Gerald Luckenbill
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try setting the preferredSize of the JLabel
i.e.
JLabel myLabel = new JLabel("Some Text");
myLabel.setPreferredSize(new Dimension(100,50));
//then add to the layout manager
I don't know what you are doing but you might want to embed your label in a JPanel to make it look better and then add the JPanel to the borderLayout - again, just make sure you set the preferred Sizes of the components or they won't show up.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David --
It is the job of the JPanel's "paint()" method to invoke the code which ends up telling the JLabel to paint itself. Labels are native operating system widgets and Java doesn't need to paint them. By overriding paint(), you're depriving the JLabel of its opportunity to paint itself.
In Swing, you never override paint(). You override paintComponent() instead; and even then, don't forget to call super.paintComponent() from your implementation so that borders, etc, will still be painted.
[ December 08, 2003: Message edited by: Ernest Friedman-Hill ]
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...that makes sense...as a follow up question, why is it that if I use Label and Button instead of JLabel and JButton, everything works? (Although they look different, of course). Was the trigger to paint these components changed in Swing vs. AWT? I'll take these comments and try to test some more. Thank you!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why is it that if I use Label and Button instead of JLabel and JButton, everything works?

AWT widgets like Label and Button have "native peers." They're linked to "real" widgets provided by the window system; Java just acts like any other application in asking X/Windows or Win32 or whatever to draw a button.
Swing widgets like JLabel and JButton are totally different; they're actually drawn in Java code. Unless the appropriate Java method gets called, they don't show up at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic