Solved

e.getSource()

Posted on 2004-09-01
13
2,557 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
  • 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
Independent Software Vendors: 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!

 
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

Industry Leaders: 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
numbers ascending pyramid 101 240
runtime exception 2 50
Need Help! Getting a syntax error and don't understand why 3 40
java mysql insert application 14 45
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

749 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