Solved

Why doesn't this work?!?!?

Posted on 2004-08-08
17
205 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
[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
  • 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
Independent Software Vendors: 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

615 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