Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why doesn't this work?!?!?

Posted on 2004-08-08
17
Medium Priority
?
207 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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…
Suggested Courses

824 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