Solved

e.getSource()

Posted on 2004-09-01
13
2,470 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO we need Java installed on a Windows PC and WHY ? 13 94
Securing Jmx Console and web console 2 64
Image decoding from Camera 3 72
jar file executable 12 37
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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…

932 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

13 Experts available now in Live!

Get 1:1 Help Now