?
Solved

JToolBar problem

Posted on 2003-03-08
11
Medium Priority
?
328 Views
Last Modified: 2008-03-10
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
Comment
Question by:asukai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:msterjev
ID: 8095220
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8095223
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
 

Author Comment

by:asukai
ID: 8095297
->
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.

 
LVL 92

Accepted Solution

by:
objects earned 320 total points
ID: 8095421
);
   
    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
 
LVL 92

Expert Comment

by:objects
ID: 8095423
Actually you may also need to do a repaint after revalidate();

toolbar.revalidate();
toolbar.repaint();
0
 
LVL 92

Expert Comment

by:objects
ID: 8095426
Perhaps a better solution though would be to use a CardLayout instead of adding/removing components.
0
 

Author Comment

by:asukai
ID: 8095914
I never used CardLayout. What kind of advantages I get in this problem?
0
 
LVL 92

Expert Comment

by:objects
ID: 8095946
> 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
 

Author Comment

by:asukai
ID: 8097876
->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
 
LVL 92

Expert Comment

by:objects
ID: 8099169
> is it possible to share componets in the CardLayout

no, a component can only belong to one container.

0
 
LVL 1

Expert Comment

by:membersh
ID: 8109405
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question