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.
-nothing important to say, but learnin' plenty-
Joined: Nov 05, 2003
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.
author and iconoclast
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 ]
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!!
author and iconoclast
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.