Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Why doesn't this work?!?!?

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!
Avatar of JugglerW
JugglerW

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");
          }
     }
     
}

============================
>Cide wtih main:

should be

Code with main:
Avatar of Unimatrix_001

ASKER

Right...thanks for that Juggler...
Ah sorry I only noticed your last comment ;)
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();
          }
     }
Why isn't it working on mine! :( :( :(
Nope still nothing...
or:

     public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().compareTo("Switch")==0)
          {
               m_type ^= 1;
               m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
               repaint();
          }
     }
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...
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();
     }
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();
     }
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.
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
ASKER CERTIFIED SOLUTION
Avatar of JugglerW
JugglerW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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
Got it sorted...it was the GridBagLayout, changed it to GridLayout and it's working!!! :S :S :S
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 :-)