Good. I'm doing an exercise that consists of generating various types of events and generating a toString() that returns a description of what generated the event. I assumed that when implementing the various interfaces I could create a variable of my class to hold the different events and call the toString() of my class but I have noticed that the type of the interface and the type of the event that is handled is not the same with what that my variable does not work. Question: Is there any way to process different events with a single String? There's the little hack I was trying...(Generator is my class and gen is the variable of it)
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
gen=(Generador)e.getSource();
System.out.println(e.getActionCommand()+""+gen.toString());
}
The toString method is a method of the Object class, which is inherited, as you know, by all classes. And by instantiating them, for all the objects you can make with java.
Some classes override this method to include the values of the various class variables. But if they are not overridden, what the toString method returns is the class of the object followed by an at sign and the hascode of the object. The hascode code is an identifier code of the memory location in which the object is stored.
To override the toString method you must do it through a public method, which returns a String and whose name is toString, without parameters.
And then it depends on what you want the message to give you. If you want it to give you the name of the object that generated the event, or any of its properties... then you will have to access that property and enter it in the return. Since you don't indicate what you want to get exactly, I can't help you more.
You can also make a return according to some condition, for example, if it was a button to get a message, if it was a textField to get another message... who knows what exactly you want to get.