Results 1 to 9 of 9
  1. #1
    apchar is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

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

    Java Code:
    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
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Well thats 4 forums. Anymore?
    Last edited by Junky; 07-05-2011 at 08:16 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Thanks Junky. Added that to the notification on the other two forums.

    db

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Well thats 3 forums. Anymore?
    Let 'im try. I'm a member almost anywhere he might.

    db

  7. #7
    apchar is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    What's the issue here guys? So what if I posted the same question on javaranch & other web sites. They're different websites with different admins & different users. Crossposting across different forums on the same site is rotten I agree. But asking multiple sources in different places is sensible. I rarely get a reply on more than 1. So where's the beef?

  8. #8
    apchar is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

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

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by apchar View Post
    What's the issue here guys?
    I see you didn't bother to read the page I linked on your JavaRanch thread. Way to go!

    db

Similar Threads

  1. Which swing component is suitable for TextEditor...?
    By Dinesh_rockz in forum AWT / Swing
    Replies: 1
    Last Post: 02-23-2011, 05:41 AM
  2. Cannot add component to JPanel
    By adi.shoukat in forum NetBeans
    Replies: 7
    Last Post: 12-10-2010, 11:12 PM
  3. swing component
    By cahyadiakbar in forum AWT / Swing
    Replies: 0
    Last Post: 09-22-2010, 10:40 AM
  4. Modifying and reloading JPanel component
    By gopher in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2009, 04:15 AM
  5. how to remove all component in container(Jpanel)
    By SaYuNaRa in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 04:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •