Solved

Why doesn't this work?!?!?

Posted on 2004-08-08
17
203 Views
Last Modified: 2010-03-31
Please see:


================================
import java.awt.*;
import java.awt.event.*;

public class JF1 extends Panel implements ActionListener{

      public JF1(){
            setup();
            position();
      }
      
      
      private Button m_switch;
      private Label m_show;
      private int m_type;
      
      public void setup(){
            m_switch=new Button("Switch");
            m_switch.setActionCommand(m_switch.getLabel());
            m_switch.addActionListener(this);
            m_show=new Label("Thing1");
            m_type=0;
      }
      
      public void position(){
            removeAll();
            setLayout(new GridBagLayout());
            GridBagConstraints c=new GridBagConstraints();
      
            c.gridx=0;
            c.gridy=0;
            c.anchor=c.WEST;
            c.insets=new Insets(10,30,0,0);
            add(m_switch, c);
            
            c.gridx=1;
            add(m_show, c);
      }
      
      public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().compareTo("Switch")==0){
                  m_type++;
                  if(m_type>1)
                        m_type=0;
                  if(m_type==0)
                        m_show.setText("Thing1");
                  else if(m_type==1)
                        m_show.setText("Thing2");
            }
      }
}
================================

The texts never change, although the m_type change works fine!

Thank you!
0
Comment
Question by:Unimatrix_001
  • 10
  • 7
17 Comments
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747830
I added a main to your code and tried and it runs without problem on my system?
Also note that I simplified your actionPerformed method slightly.

Cide wtih main:

============================
import java.awt.*;
import java.awt.event.*;

public class JF1 extends Panel implements ActionListener{
    public static void main(String[] args)
    {
        Frame f = new Frame();
        f.setSize(200,200);
        f.add( new JF1() );
        f.setVisible(true);
    }

     public JF1(){
          setup();
          position();
          show();
     }
     
     
     private Button m_switch;
     private Label m_show;
     private int m_type;
     
     public void setup(){
          m_switch=new Button("Switch");
          m_switch.setActionCommand(m_switch.getLabel());
          m_switch.addActionListener(this);
          m_show=new Label("Thing1");
          m_type=0;
     }
     
     public void position(){
          removeAll();
          setLayout(new GridBagLayout());
          GridBagConstraints c=new GridBagConstraints();
     
          c.gridx=0;
          c.gridy=0;
          c.anchor=c.WEST;
          c.insets=new Insets(10,30,0,0);
          add(m_switch, c);
         
          c.gridx=1;
          add(m_show, c);
     }
     
     public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().compareTo("Switch")==0)
          {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
          }
     }
     
}

============================
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747832
>Cide wtih main:

should be

Code with main:
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747835
Right...thanks for that Juggler...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747838
Ah sorry I only noticed your last comment ;)
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747846
If the app not works on your system you may try:

     public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().compareTo("Switch")==0)
          {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
               validate();
          }
     }
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747849
Why isn't it working on mine! :( :( :(
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747854
Nope still nothing...
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747863
or:

     public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().compareTo("Switch")==0)
          {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
               repaint();
          }
     }
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747874
Nah...I've put this in:

     public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().compareTo("Switch")==0)
          {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
          }
          System.out.println(m_show.getText());
     }

And it's just not changing at all...
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747888
Just for a test try this. If this works something with the compare goes wrong???

     public void actionPerformed(ActionEvent e)
     {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
               System.out.println(m_show.getText());
               repaint();
     }
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747894
Some more test outputs:

    public void actionPerformed(ActionEvent e)
     {
               System.out.println("ActionCommand is " + e.getActionCommand());
               m_type ^= 1; // Note the ^  this is a XOR operator
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
               System.out.println("m_Type = " + m_Type + " text = " + m_show.getText());
               repaint();
     }
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11747925
To be sure the event is from your switch button you may also use:

    public void actionPerformed(ActionEvent e)
     {
       if ( e.getSource() == m_switch )
       {
           m_type ^= 1;
           m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
           System.out.println("m_Type = " + m_type + " text = " + m_show.getText());
           repaint();
       }
     }

and it's faster as the string compare.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11747960
I've already tried that, and it's not even picking up the button then!

Although System.out gave:
show = java.awt.Label[label2,0,0,0x0,invalid,align=left,text=Thing1] text = Thing2
0
 
LVL 7

Accepted Solution

by:
JugglerW earned 500 total points
ID: 11747967
Here again the whole program as it works for me. With every press to button the text changes from Thing1 to Thing2. Need some sleep now. CU and Good luck.

import java.awt.*;
import java.awt.event.*;

public class JF1 extends Panel implements ActionListener
{
    //~ Instance fields --------------------------------------------------------------------------------------------------------------------

    private Button m_switch;
    private Label  m_show;
    private int    m_type;

    //~ Constructors -----------------------------------------------------------------------------------------------------------------------

    public JF1(  )
    {
        setup(  );
        position(  );
    }

    //~ Methods ----------------------------------------------------------------------------------------------------------------------------

    public void setup(  )
    {
        m_switch = new Button( "Switch" );
        m_switch.addActionListener( this );
        m_show     = new Label( "Thing1" );
        m_type     = 0;
    }

    public void position(  )
    {
        removeAll(  );
        setLayout( new GridBagLayout(  ) );

        GridBagConstraints c = new GridBagConstraints(  );

        c.gridx      = 0;
        c.gridy      = 0;
        c.anchor     = GridBagConstraints.WEST;
        c.insets     = new Insets( 10, 30, 0, 0 );
        add( m_switch, c );

        c.gridx = 1;
        add( m_show, c );
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource(  ) == m_switch )
        {
            m_show.setText( ( m_type ^= 1 ) == 0 ? "Thing1" : "Thing2" );
        }
    }

    public static void main( String[] args )
    {
        Frame f = new Frame(  );
        f.setSize( 200, 200 );
        f.add( new JF1(  ) );
        f.setVisible( true );
    }
}
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11748004
> show = java.awt.Label[label2,0,0,0x0,invalid,align=left,text=Thing1] text = Thing2

Seems your label has not size => not layouted correctly.

Compare your code with mine above must be a difference ????

CU
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11748048
Got it sorted...it was the GridBagLayout, changed it to GridLayout and it's working!!! :S :S :S
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11750085
Hmmmm? I do not see any reason for this.
Whats your JDK version (java -version), platform (Windows, Linux, ...)?
My code works as listed above with the GridBagLayout. GridLayout is not so nice because it allows only a foxed grid of rows and columns.

Thanks for the points :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 42
add projects t working set in maven 2 37
Selenium docs api java index 3 64
Need help aligning JRadioButton that put in a box using GridBagLayout 2 27
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

685 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