Solved

e.getSource()

Posted on 2004-09-01
13
2,438 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
Comment Utility
The getSource() method returns the Object on which the event  occurred (not an int).
0
 
LVL 1

Author Comment

by:meow00
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

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

Assisted Solution

by:expertmb
expertmb earned 100 total points
Comment Utility
>>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
Comment Utility
if(((JButton)e.getSource()).getText() == "1")
myString +=1;
if(((JButton)e.getSource()).getText() == "2")
myString +=2;
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now