Solved

Why doesn't this work?!?!?

Posted on 2004-08-08
17
204 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
How to convert from xls to xlsx using java 7 158
How to configure empty element in XML Document parser? 15 58
hashmap order 17 62
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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