Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: can't add to JPanel after removeAll() is triggered by another swing component

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question can't add to JPanel after removeAll() is triggered by another swing component

    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
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: can't add to JPanel after removeAll() is triggered by another swing component

    Why do you want to delete the label? Just call setText.
    Improving the world one idiot at a time!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: can't add to JPanel after removeAll() is triggered by another swing component

    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't add to JPanel after removeAll() is triggered by another swing component

    Junky, as I mentioned, the actual app is more complicated. I thought it prudent to boil it down to as little as possible.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't add to JPanel after removeAll() is triggered by another swing component

    for the archives: correct code is:
    	private void changeLabel()
    	{
    		System.out.println("language = " + language);
     
    		jPanel1.removeAll();
     
    		jPanel1.add(new JLabel(language), "Center");
    		jPanel1.revalidate();
    		repaint();
    	}

    Thanks to stanislavL.

Similar Threads

  1. Java Tip Jul 29, 2010 - Swing Console Component
    By helloworld922 in forum Java Swing Tutorials
    Replies: 6
    Last Post: April 16th, 2014, 12:08 AM
  2. Replies: 0
    Last Post: November 27th, 2010, 10:47 PM
  3. Java Tip Jul 29, 2010 - Swing Console Component
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: August 24th, 2010, 06:48 AM
  4. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM