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 JLabel not showing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel not showing" Watch "JLabel not showing" New topic
Author

JLabel not showing

David Luna
Greenhorn

Joined: Feb 26, 2004
Posts: 2
Hi there,
I'm extremely new to Java (started last week) and this looks like a nice place to get some help... soooo, here's my problem:
I've set up an application that loads up a class named GUI(), which extends JPanel, and sorted out a GUI using GridBagLayout. Everything goes into place fine, except for (the only) JLabel (that I've tried to use).
Here's the code snippet:
public class GUI extends JPanel {
...
private JLabel textLabel;
private JTextField titleInputArea;
...
public GUI(JFrame frame) {
super(new GridBagLayout());
...
textLabel = new JLabel("Title: ");
textLabel.setLabelFor(titleInputArea);
titleInputArea = new JTextField("Enter title here");
...
GridBagConstraints c = new GridBagConstraints();
...
c.gridy = 1;
c.gridx = 0;
add(textLabel, c);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = 4;
c.gridy = 1;
c.gridx = 1;
add(titleInputArea, c);
...
}
}
The text entry box (titleInputArea) shows fine, but the JPanel saying "Title: " refuses to show.
Thanks in advance for any help you can offer
- David
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I ran your GUI JPanel, loaded into the JFrame in GUITest below and the JLabel and JTextField both showed up fine.
Here's a variation using the weightx constraint to control component size; not always a good idea but it resizes well.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Personally, I would recommend using an easier layout when you are first starting with Java. FlowLayout, BoxLayout, GridLayout, etc. until you understand the whole Layout concept. I have been programming in Java for about 7 years and have never had to use GridBaglayout. Both the Swing apps I have released ( http://www.filenabber.com ) do NOT use GridBag. Try one of hte other layouts to start with.
Just my $0.02
Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
David Luna
Greenhorn

Joined: Feb 26, 2004
Posts: 2
Hi there, this is going ot sound realy stupid but...
After I posted on here I had a couple of difficulties with the code. First of all I'll inform you that I'm using Xcode on Mac OS X Panther. Firstly I changed a couple of the strings, after deciding that I wanted different text to appear in the main JTextArea on startup. I was rather miffed when the next text didn't show up. I changed the code again and again, in an attempt to try to figure out why my compiler seemed to be 'remembering' the old string (even though it now existed nowhere in the Xcode Project). After playing with the code for about half an hour I tried to compile it again but this time something very odd happened; I got the error message:
Exception in thread "main" java.lang.NullPointerException
at apple.launcher.LaunchRunner.run(LaunchRunner.java:85)
at apple.launcher.LaunchRunner.callMain(LaunchRunner.java:50)
at apple.launcher.JavaApplicationLauncher.launch(JavaApplicationLauncher.java:52)
Now if I'm honest I don't have a clue what this meant, but it stopped my application from running, so I'm guessing it isn't good.
After copying the contents of the Java files, quitting Xcode, deleting the old versions of the files, creating a new project in Xcode and copying/pasting the old code back into a new project it started to run again.
Now when I load up the application it says:
-[NSJavaVirtualMachine initWithClassPath:] cannot instantiate a Java virtual machine
but continues to load it up as normal.
It also now loads the JLabels.
If anyone has an explination for this, or has heard of this happening elsewhere please tell me - I'm intruiged to know why Xcode is blocking my otherwise fine Java from compiling.
Yours - David
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLabel not showing