Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI compiles with no errors, but sometimes shows nothing

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);

} 
like image 740
FossilizedCarlos Avatar asked Mar 31 '12 00:03

FossilizedCarlos


People also ask

How to use listModel in Java?

Method SummaryAdds a listener to the list that's notified each time a change to the data model occurs. Returns the value at the specified index. Returns the length of the list. Removes a listener from the list that's notified each time a change to the data model occurs.

What is look and feel in Java?

“Look” refers to the appearance of GUI widgets and “feel” refers to the way the widgets behave. Sun's JRE provides the following L&Fs: CrossPlatformLookAndFeel: this is the “Java L&F” also known as “Metal” that looks the same on all platforms. It is part of the Java API (javax. swing.


1 Answers

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.

like image 106
Hovercraft Full Of Eels Avatar answered Oct 06 '22 18:10

Hovercraft Full Of Eels