views:

288

answers:

4

I am writting a simple application which has a button that opens a new window then display a simple GUI/Text to acccept inputs from a user. but for some reason, I can get JLabel to be displayed on the new window. The application has following structure:

+mainFrame - JFrame
+newFrame - JFrame
-+newPanel - JPanel
----title - JLabel
----submitButton -JButton
...

Buttons and textfields all display fine, but Jlabels won't show up at all. I have tried using different layouts and all but I still can't get it shown. JLabels inside mainFrame tree, works fine.. so it seems like the problem is due to newFrame declaration or something, but then button should not be displayed either. Well, I am kindda lost and can someone suggest me what I should check?

Thanks : )

+1  A: 

Make sure you do frame.pack() before you make it visible.

It can also help to set borders on different components (in different colours) for debugging just to see which components are/aren't turning out with size 0, in order to narrow down your problem. Logging, or breakpointing the component's setSize method, can help too.

Apart from that, maybe post some sample code? At the moment, you're question is fairly vague to answer.

William Billingsley
+1  A: 

Firstly, do you know about JDialog, and JOptionPane - these classes are often a better way of showing another popup window. It is quite rare to use 2 JFrames, (though sometimes a sensible thing to do).

Secondly have you done pack() and setVisible(true)?

The code below works fine for me. Either this breaks for you and it is something about your Java implementation, or you must be doing something different, in which case can you tell us what it is:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class JLabelShower {
  public static void main(String [] args) {
    JFrame mainFrame = new JFrame("main frame");
    JButton popup = new JButton("start new frame");
    mainFrame.getContentPane().add(popup);
    mainFrame.pack();
    mainFrame.setVisible(true);

    popup.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFrame newFrame = new JFrame("new frame");
        JPanel newPanel = new JPanel();
        JLabel title = new JLabel("title");
        newPanel.add(title);
        newFrame.setContentPane(newPanel);
        newFrame.pack();
        newFrame.setVisible(true);
      }
    });
  }
}
Nick Fortescue
A: 

In case you are using the JLabel as a placeholder, i.e. initialize it with an empty string and set the text later:

Since the JLabel's size gets calculated when the panel gets layed out (i.e. early on) and is based on the contained text, you'll probably end up with a label thinking it has a preferred size of (0, 0).
In this case you should tell the label what size it should ask for by calling setPreferredSize with an appropriate value.

And another cause might be the layoutmanager you are using in the surrounding panel. Maybe you are adding the label and the button in the same place, e.g. BorderLayout.CENTER. That would explain why only one of the two gets displayed.

Stroboskop
A: 

Thanks a lot everyone for help! It turns out that there was a mistake in JLabel declaration. doh! :(

so everything works just fine now : )

steeldusk