1

For some reason my JLabel isn't displaying and I can't figure out why (editString DOES have a value).

compFrame.removeAll();
JPanel editPane = new JPanel();
editPane.setLayout(new GridLayout(0,1));
compFrame.add(editPane);
//JLabel lastValue = new JLabel(editString);
editPane.add(new JLabel(editString));

compFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
compFrame.setVisible(true);
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
skirsch00
  • 29
  • 8
  • 1
    Why are using `compFrame.removeAll();`. Remove that line. It will work – K Adithyan Aug 17 '18 at 20:00
  • I needed to clear it because I'm using it elsewhere. Didn't know I had to add getContentPane(). – skirsch00 Aug 17 '18 at 20:15
  • `compFrame.removeAll();` 1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) In future, please post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 18 '18 at 03:45

1 Answers1

1

you should use removeAll on ContentPane. Try this

compFrame.getContentPane().removeAll();

The API doc for removeAll says:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

Shahid
  • 481
  • 1
  • 8
  • 22