6

I'm using a custom class that extends JFrame, but sometimes it shows nothing. I never get any faults, so I'm curious if the is a java command that can help me print something. I looked around for other questions, but found nothing similar. Not really doing anything too crazy, but curious as to why this happens. I would like to correct the problem to avoid future problems.


Blank
enter image description here
GUI
enter image description here

public MemberPanel(int i) throws IOException {
  Container contentPane = getContentPane();
  GridLayout layout = new GridLayout(2, 1);
  contentPane.setLayout(layout);
  setVisible(true);
  setLocation(0, 0);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(640, 170);
  setResizable(false);

  greenStatus = new JButton("Non-Critical");
  yellowStatus = new JButton("Important");
  redStatus = new JButton("Mission Critical");

  greenStatus.setFont(fontTextOne);
  yellowStatus.setFont(fontTextOne);
  redStatus.setFont(fontTextOne);

  greenStatus.addActionListener(this);
  yellowStatus.addActionListener(this);
  redStatus.addActionListener(this);

  buttonPanel.add(greenStatus);
  buttonPanel.add(yellowStatus);
  buttonPanel.add(redStatus);

  statusLabel = new JLabel("In 75 letters or less... What are you working on?");
  statusLabel.setVerticalAlignment(JLabel.CENTER);
  statusLabel.setHorizontalAlignment(JLabel.CENTER);
  statusLabel.setFont(fontTextTwo);
  textFieldPanel.add(statusLabel);
  textFieldPanel.add(statusMessage);

  contentPane.add(buttonPanel);
  contentPane.add(textFieldPanel);

} 
Charles
  • 50,943
  • 13
  • 104
  • 142
FossilizedCarlos
  • 197
  • 2
  • 10
  • Swing GUI objects should be constructed and manipulated _only_ on the [event dispatch thread](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Mar 31 '12 at 00:56

1 Answers1

10

You're adding a bunch of components after calling setVisible(true) on the JFrame:

public MemberPanel(int i) throws IOException {
  Container contentPane = getContentPane();
  GridLayout layout = new GridLayout(2, 1);
  contentPane.setLayout(layout);
  setVisible(true);  // ****** here

  // .....

  // only now do you add components...
  contentPane.add(buttonPanel);
  contentPane.add(textFieldPanel);

} 

And so the components may or may not show depending on whether the GUI repaints or not (see what happens when you re-size the empty gui). Fix: call setVisible(true) only after adding everything.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Should he even be extending `JFrame`? Why not just add your UI to the content pane of a regular frame. – Nate W. Mar 31 '12 at 00:51
  • @Shakedown: What is a "*regular* frame? I've never heard of this. – Hovercraft Full Of Eels Mar 31 '12 at 00:52
  • @Shakedown: Yes, I'm a big believer in not extending components if you're not altering their fundamental behaviors, so yes, he would probably be better off *using* a JFrame rather than *extending* a JFrame, but that is not the main cause of his current problem. – Hovercraft Full Of Eels Mar 31 '12 at 00:55
  • Well, a regular frame would be `new JFrame()`. But, come to think of it, I've extended `JFrame` before and I don't feel like that's incorrect or wrong as long as you actually need to modify the behavior. Either way, you're right, it's not the real problem here. – Nate W. Mar 31 '12 at 00:55
  • @Hovercraft Full Of Eels: Interesting. I moved the JFrame calls to the end, and the empty GUI has not reappeared. – FossilizedCarlos Mar 31 '12 at 02:05
  • 2
    @PetroEkos This answer is completely correct. If you look at the sample code I wrote in [this answer](http://stackoverflow.com/questions/9885855/how-to-dynamically-add-jbutton-to-jpanel/9885927#9885927) you can see what happens if you dynamically add components (as you did by first making the frame visible), and what is needed to get them to show up – Robin Mar 31 '12 at 06:18