Unimatrix_001
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.g etLabel()) ;
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(ActionEven t e){
if(e.getActionCommand().co mpareTo("S witch")==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!
==========================
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.addActionListener
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(ActionEven
if(e.getActionCommand().co
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!
>Cide wtih main:
should be
Code with main:
should be
Code with main:
ASKER
Right...thanks for that Juggler...
ASKER
Ah sorry I only noticed your last comment ;)
If the app not works on your system you may try:
public void actionPerformed(ActionEven t e){
if(e.getActionCommand().co mpareTo("S witch")==0 )
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
validate();
}
}
public void actionPerformed(ActionEven
if(e.getActionCommand().co
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
validate();
}
}
ASKER
Why isn't it working on mine! :( :( :(
ASKER
Nope still nothing...
or:
public void actionPerformed(ActionEven t e){
if(e.getActionCommand().co mpareTo("S witch")==0 )
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
repaint();
}
}
public void actionPerformed(ActionEven
if(e.getActionCommand().co
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
repaint();
}
}
ASKER
Nah...I've put this in:
public void actionPerformed(ActionEven t e){
if(e.getActionCommand().co mpareTo("S witch")==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...
public void actionPerformed(ActionEven
if(e.getActionCommand().co
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
}
System.out.println(m_show.
}
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(ActionEven t e)
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
System.out.println(m_show. getText()) ;
repaint();
}
public void actionPerformed(ActionEven
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
System.out.println(m_show.
repaint();
}
Some more test outputs:
public void actionPerformed(ActionEven t e)
{
System.out.println("Action Command 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();
}
public void actionPerformed(ActionEven
{
System.out.println("Action
m_type ^= 1; // Note the ^ this is a XOR operator
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
System.out.println("m_Type
repaint();
}
To be sure the event is from your switch button you may also use:
public void actionPerformed(ActionEven t 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.
public void actionPerformed(ActionEven
{
if ( e.getSource() == m_switch )
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
System.out.println("m_Type
repaint();
}
}
and it's faster as the string compare.
ASKER
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,invali d,align=le ft,text=Th ing1] text = Thing2
Although System.out gave:
show = java.awt.Label[label2,0,0,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> show = java.awt.Label[label2,0,0, 0x0,invali d,align=le ft,text=Th ing1] text = Thing2
Seems your label has not size => not layouted correctly.
Compare your code with mine above must be a difference ????
CU
Seems your label has not size => not layouted correctly.
Compare your code with mine above must be a difference ????
CU
ASKER
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 :-)
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 :-)
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.addActionListener
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(ActionEven
if(e.getActionCommand().co
{
m_type ^= 1;
m_show.setText(m_type == 0 ? "Thing1" : "Thing2");
}
}
}
==========================