JToolBar problem

When I try to change components in JToolBar later,  it does not seems to give the correct result. In the program below, if you click the JButton twice you get unexpected result. Can anyone tell me how to overcome this problem?

Thanx
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Test3
{

    public static void main(String[] args)
    {
     TestFrame frame=new TestFrame();
     frame.show();
    }

}

class TestFrame extends JFrame
{
    JToolBar toolBar;
    boolean boo=true;
    TestFrame()
    {
     setSize(500, 500);
     toolBar= new JToolBar();
     toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
     

     toolBar.add(new JButton("Test1"));
     toolBar.add(new JTextField(2));
     toolBar.add(new JLabel("px"));
     Container contentPane = getContentPane();
     contentPane.add(toolBar,BorderLayout.NORTH);
     
     JButton button=new JButton("Change ToolBar");
     button.addActionListener(new
                     ActionListener()
         {
          public void actionPerformed(ActionEvent event)
          {
               toolBar.removeAll();
               
          if(boo)
              {

               toolBar.add(new JLabel("px"));

               toolBar.add(new JButton("Test2"));
               boo=false;
              }
          else
              {
               toolBar.add(new JButton("Test1"));
               toolBar.add(new JTextField(2));
               toolBar.add(new JLabel("px"));
               boo=true;    
              }
          }
         });
         
     contentPane.add(button,BorderLayout.CENTER);
     
         
    }


}
asukaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

msterjevCommented:
Use:
toolBar.invalidate();

here:

public void actionPerformed(ActionEvent event)
         {
              toolBar.removeAll();
             
         if(boo)
             {

              toolBar.add(new JLabel("px"));

              toolBar.add(new JButton("Test2"));
              boo=false;
             }
         else
             {
              toolBar.add(new JButton("Test1"));
              toolBar.add(new JTextField(2));
              toolBar.add(new JLabel("px"));
              boo=true;    
             }
         }
         toolBar.invalidate();
        });
0
msterjevCommented:
Little mistake:

public void actionPerformed(ActionEvent event)
        {
             toolBar.removeAll();
             
        if(boo)
            {

             toolBar.add(new JLabel("px"));

             toolBar.add(new JButton("Test2"));
             boo=false;
            }
        else
            {
             toolBar.add(new JButton("Test1"));
             toolBar.add(new JTextField(2));
             toolBar.add(new JLabel("px"));
             boo=true;    
            }
               toolBar.invalidate();
        }
         });
0
asukaiAuthor Commented:
->
It does not make any difference. The TextField is always displayed....
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

objectsCommented:
);
   
    JButton button=new JButton("Change ToolBar");
    button.addActionListener(new
                    ActionListener()
        {
         public void actionPerformed(ActionEvent event)
         {
              toolBar.removeAll();
             
         if(boo)
             {

              toolBar.add(new JLabel("px"));

              toolBar.add(new JButton("Test2"));
              boo=false;
             }
         else
             {
              toolBar.add(new JButton("Test1"));
              toolBar.add(new JTextField(2));
              toolBar.add(new JLabel("px"));
              boo=true;    
             }
         }
         toolbar.revalidate();
        });
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
Actually you may also need to do a repaint after revalidate();

toolbar.revalidate();
toolbar.repaint();
0
objectsCommented:
Perhaps a better solution though would be to use a CardLayout instead of adding/removing components.
0
asukaiAuthor Commented:
I never used CardLayout. What kind of advantages I get in this problem?
0
objectsCommented:
> What kind of advantages I get in this problem?

You don't need to add/remove components each time. You would just need to switch 'cards'.

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

0
asukaiAuthor Commented:
->objects I tried the CardLayout.One question, is it possible to share componets in the CardLayout? For example, in the code below, two panel shares teh JButton "b1". When I tried it the button "b1" didn't display in the panel "p1" but "p2".


        cards = new JPanel();
        cards.setLayout(new CardLayout());
   JButton b1=new JButton("Button 1");
        JPanel p1 = new JPanel();
        p1.add(b1);
 

        JPanel p2 = new JPanel();
        p2.add(new JTextField("TextField", 20));
p2.add(b1);
        cards.add(p1, BUTTONPANEL);
        cards.add(p2, TEXTPANEL)
0
objectsCommented:
> is it possible to share componets in the CardLayout

no, a component can only belong to one container.

0
membershCommented:
Please try this
     public void actionPerformed(ActionEvent event)
         {
              toolBar.removeAll();
              toolBar.invalidate();
              toolBar.validate();
              toolBar.repaint() ;
         if(boo)
             {
              toolBar.add(new JLabel("px1"));
              toolBar.add(new JButton("Test22"));
              boo=false;
             } else {
              toolBar.add(new JButton("Test111"));
              toolBar.add(new JTextField());
              toolBar.add(new JLabel("px222"));
              boo=true;    
             }
         }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.