meow00
asked on
e.getSource()
Hi Experts,
What did I do wrong in the following line 01 ? and how do I fix it ?
where I have :
b1 = new JButton("1"); b1.addActionListener(this)
b2 = new JButton("2"); b2.addActionListener(this)
--------------------------
public void actionPerformed(ActionEven
n++ ;
switch((int)e.getSource())
case '1' : myString += '1'; break ;
case '2' : myString += '2'; break ;
}
--------------------------
please help !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it returns the object , which is a component.
instead use
b1.setName("button1");
b2.setName("button2");
if(e.getSource() instanceof JButton) {
if(((JButton)e.getSource() ).getName( ) == "button1")
dosomething
instead use
b1.setName("button1");
b2.setName("button2");
if(e.getSource() instanceof JButton) {
if(((JButton)e.getSource()
dosomething
Where is the full code ? and what's the main problem ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if(((JButton)e.getSource() ).getText( ) == "1")
myString +=1;
if(((JButton)e.getSource() ).getText( ) == "2")
myString +=2;
myString +=1;
if(((JButton)e.getSource()
myString +=2;
public void actionPerformed(ActionEven t e) {
n++ ;
switch((int)e.getSource(). getText(). toString() .charAt(0) ){
case '1' : myString += '1'; break ;
case '2' : myString += '2'; break ;
}
n++ ;
switch((int)e.getSource().
case '1' : myString += '1'; break ;
case '2' : myString += '2'; break ;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or you may try like this
public void actionPerformed(ActionEven t e) {
n++ ;
switch(e.getSource().getNa me().toStr ing().char At(0)){
case '1' : myString += '1'; break ;
case '2' : myString += '2'; break ;
}
public void actionPerformed(ActionEven
n++ ;
switch(e.getSource().getNa
case '1' : myString += '1'; break ;
case '2' : myString += '2'; break ;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
ASKER
if(e.getSource() == b1) {} ... I can't use switch ???
that seems to be complicated to use a lot of if & else .... is there a better way to use switch here ?
Thanks !