Consider the bit of code below. It's a much simplified version of my real app.
In jPanel1 is a single label. In jPanel2 is a single button.
The button in jPanel2 is supposed to wipe clear (with removeAll()) jPanel1 and add a new label in place of the old one. What actually happens is that after the first button press, jPanel1 is indeed cleared by the removeAll() method but the new label can't be added (or rather it can be added but won't show.) I can hard code the removal & addition back and forth all day & it works fine. It's just when I use a swing component like JButton or JComboBox that it doesn't work. Why?? Is this a thread thing? (I know I can just change the text of the label but the real app is much more complicated.)
package my.stuff; import java.awt.*; import javax.swing.*; public class TreeTest3 extends javax.swing.JFrame { private String language = "english"; public TreeTest3() { initComponents(); jPanel1.setLayout(new FlowLayout()); jPanel1.setVisible(true); changeLabel(); } private void changeLabel() { System.out.println("language = " + language); jPanel1.removeAll(); jPanel1.validate(); jPanel1.add(new JLabel(language), "Center"); repaint(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(new javax.swing.border.MatteBorder(null)); jPanel1.setPreferredSize(new java.awt.Dimension(400, 204)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 398, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 133, Short.MAX_VALUE) ); jButton1.setText("switch"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1) .addContainerGap(308, Short.MAX_VALUE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1) .addContainerGap(126, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (language.equals("english")) language = "spanish"; else language = "english"; changeLabel(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TreeTest3().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration }