Solved

e.getSource()

Posted on 2004-09-01
13
2,601 Views
Last Modified: 2012-06-27

 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); Pane2.add(b1) ;
    b2 = new JButton("2");  b2.addActionListener(this); Pane2.add(b2) ;
------------------------------------------------------------------
  public void actionPerformed(ActionEvent e) {
        n++ ;    
            switch((int)e.getSource()){   //line 01   inconvertible types, found : java.Lang.Object, require : int
            case '1' : myString += '1'; break ;
            case '2' : myString += '2'; break ;
        }
       
----------------------------------------------------------------------
 please help !
0
Comment
Question by:meow00
[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
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 7

Accepted Solution

by:
lhankins earned 50 total points
ID: 11960337
The getSource() method returns the Object on which the event  occurred (not an int).
0
 
LVL 1

Author Comment

by:meow00
ID: 11960351
so .... does this mean : I must use

 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 !
 
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11960352
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
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 15

Expert Comment

by:Javatm
ID: 11960411
Where is the full code ? and what's the main problem ?
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 11960439
>>if(e.getSource() == b1) {} ... I can't use switch ???
you cant use switch here.

>>that seems to be complicated to use a lot of if & else
ya you need lots of if and else if.

other way will be you have to write this  for which ever component you want to register the listener.
b1.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        //dosoething                  }
            });
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11960488
if(((JButton)e.getSource()).getText() == "1")
myString +=1;
if(((JButton)e.getSource()).getText() == "2")
myString +=2;
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11960502
public void actionPerformed(ActionEvent e) {
        n++ ;    
            switch((int)e.getSource().getText().toString().charAt(0)){  
            case '1' : myString += '1'; break ;
            case '2' : myString += '2'; break ;
        }
0
 
LVL 18

Assisted Solution

by:armoghan
armoghan earned 50 total points
ID: 11960504
You can also use switch if you are sure that you will get 1, 2,3, etc
Convert the

int i = Integer.parseInt(((JButton)e.getSource()).getText() ) )
and then use it in switch
 
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 50 total points
ID: 11960506
public void actionPerformed(ActionEvent e) {
        n++ ;    
            switch(e.getSource().getText().toString().charAt(0)){  
            case '1' : myString += '1'; break ;
            case '2' : myString += '2'; break ;
        }
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 11960520
you can do something like this

getText() will return the text written on the button which will be meaning ful name

use
b1.setName("1");
b2.setName("2");
b3.setName("3");

 
public void actionPerformed(ActionEvent e) {
            switch(((JButton)e.getSource()).getName().charAt(0)){  
            case '1' : myString += '1'; break ;
            case '2' : myString += '2'; break ;
          default:
 break;
        }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11960532
or you may try like this

 public void actionPerformed(ActionEvent e) {
        n++ ;    
            switch(e.getSource().getName().toString().charAt(0)){  
            case '1' : myString += '1'; break ;
            case '2' : myString += '2'; break ;
        }
0
 
LVL 1

Assisted Solution

by:talvio
talvio earned 50 total points
ID: 11960535
Another example how to go with listener implementation in addition to an anonymous class -in expertmb's example- is to actually create you own class:
(could be inner class or maybe a package protected to remove it from your public API if necessary -here defined public for simplicity)
public class MyListener implements Listener {
   private action_id = -1;
   public MyListener(int id) {
      this.action_id = id;
   }
   public void actionPerformed(ActionEvent e) {
      switch (this.private_id) {
          case 0 :
               // do something
          case 1 :
               // do something else
          default :
               // do yet something else
       }
   }
}

Now you chagen
b2 = new JButton("2");  b2.addActionListener(this); Pane2.add(b2) ;
into
b2 = new JButton("Two"); b2.addActionListener(new MyListener(2)); Pane2.add(b2);

This is a nice design if those actions are somewhat similar and have common functionality -maybe use same private methods- and works of course in any case, but to keep desing nice and clean I wouldn't put all action code in a single class atleast if the class starts to grow really large.
-jT
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11961031
:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

695 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