Solved

Why doesn't this work?!?!?

Posted on 2004-08-08
17
198 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now