Reputation: 197
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
GUI
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);
}
Upvotes: 6
Views: 2565
Reputation: 285405
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.
Upvotes: 10