?
Solved

e.getSource()

Posted on 2004-09-01
13
Medium Priority
?
2,630 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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 200 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 200 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 400 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

764 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