File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Can't see my Panels!!! Aaarrghhhhh.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic
Author

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

David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
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.


David Crossett
-nothing important to say, but learnin' plenty-
Gerald Luckenbill
Greenhorn

Joined: Nov 05, 2003
Posts: 15
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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 ]

[Jess in Action][AskingGoodQuestions]
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
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

Joined: Jul 08, 2003
Posts: 24166
    
  30


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't see my Panels!!! Aaarrghhhhh....
 
Similar Threads
JButton/JLabel Problem
Instance Variable errors for MouseMotionAdapt
Button not appearing and drawString issue
Weird error not sure what Im doing wrong
code not working