Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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);
     
         
    }


}
0
asukai
Asked:
asukai
  • 5
  • 3
  • 2
  • +1
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now