4 Replies - 354 Views - Last Post: 01 February 2013 - 04:00 AM Rate Topic: -----

#1 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 154
  • View blog
  • Posts: 602
  • Joined: 24-April 10

Why does a cast need to happen in this code?

Posted 31 January 2013 - 02:12 AM

Hey,

I had a problem with my code and I found exactly what I needed to fix it, but the problem is I don't udnerstand why it works. I've looked all over but can't find a good explanation, could anyone here help?

public class MyButton extends JButton {

private int id;

// ...

public int returnID(){
return id;
}

}


MyButton myButton = new MyButton();
// ...
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyButton button = (MyButton) event.getSource();
button.returnID();
// Do something with x and y
}
});[/code]

in the actionPerformed, why is the the event cast to a button?

public class MyButton extends JButton {

private int id;

// ...

public int returnID(){
return id;
}

}


MyButton myButton = new MyButton();
// ...
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyButton button = (MyButton) event.getSource();
button.returnID();
// Do something with x and y
}
});



Is This A Good Question/Topic? 0
  • +

Replies To: Why does a cast need to happen in this code?

#2 erik venn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 17-January 13

Re: Why does a cast need to happen in this code?

Posted 31 January 2013 - 02:17 AM

can u show me what is in ur jbutton class.
Was This Post Helpful? 0
  • +
  • -

#3 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 154
  • View blog
  • Posts: 602
  • Joined: 24-April 10

Re: Why does a cast need to happen in this code?

Posted 31 January 2013 - 02:48 AM

for now it is as in my code above

	public class MyButton extends JButton {
	 
	private int id;
	 
	// ...
	 
	public int returnID(){
	return id;
	}
	 
	}


Was This Post Helpful? 0
  • +
  • -

#4 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: Why does a cast need to happen in this code?

Posted 31 January 2013 - 07:28 AM

View Postmike73, on 31 January 2013 - 11:12 AM, said:

in the actionPerformed, why is the the event cast to a button?

It's not the "event" that is returned, but the object where the event occurred ... the cast is necessary because it returns a generic Object, instead of the exact original object type...
Was This Post Helpful? 4
  • +
  • -

#5 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Why does a cast need to happen in this code?

Posted 01 February 2013 - 04:00 AM

In JAVA, Every class extends the Object class


genneral var=(general) specific
general g=specific

so it is

GGS-GS

for a class me that eventually extends Object

Object o=(Object) new me();
Object o2=new me();

This post has been edited by burakaltr: 01 February 2013 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1