ActionEvent confusion - java

I'm very new to Java and I'm confused about the ActionEvent class.
I was under the impression that to use a class you had to create a new object for example:
SomeClass aClass = new SomeClass();
But in this piece of code:
private class theHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
Event.getSource()
// etc...
}
}
How come you don't need to:
ActionEvent event = new ActionEvent();
Event become an instance of the ActionEvent class without having explicitly tell it.
I'm sorry of this is a dumb question, I'm teaching my self Java, and this is thoroughly confusing to me.

ActionEvent is created somewhere, but it's just not being created explicitly by you. Rather, it's being created in the JButton's code (actually the code is in the AbstractButton class, a parent class for JButton) for notifying all ActionListeners that listener are attached to it. You can view the source file to see the details if you desire.

When you use implements in Java, what you are doing is using an interface. Interfaces require certain methods to be implemented in your code and offer a way to ensure new classes that implement a certain interface have methods that can be called.
Thus, in your example:
private class theHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
event.getSource()
// etc...
}
}
The ActionListener interface is being implemented by theHandler and therefore the method, actionPerformed(ActionEvent event) is required to be implemented by your class. There is no explicit ActionEvent event = new ActionEvent(); being created here because the ActionEvent is actually being passed through as a parameter to actionPerformed and is otherwise being created somewhere else.
Specifically for your example, you can also do the following instead of using the implements keyword on your class:
public class theHandler {
public theHandler() {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// do some stuff here
}
});
}
}

It's really quite simple, there is a thing called static methods. These are methods you can use from a class without making objects of them.
Like the Math class and all it's functions fx. a static method could look like this:
public class HiClass {
public static String sayHi() {
return "Hi!";
}
}
You would then use it in some context like this:
System.out.println( HiClass.sayHi() );
This would print out the string the method returns in your console.
You might ask "why wouldnt I just make all my methods in my classes static then?" there are many reasons, main one I can think of is that when you program you always try to restrcit responsibilities to classes that makes sense, so that the code is more manageable and easier to alter as the program grows.
This is not specific to the context your mentioning though, but it seemed to me your real question is how you can use methods without creating an object?
Hope it answers your question?

Related

implement ActionListener in the same class for three buttons

I have three buttons in a frame, two of which I want to edit a String with, which is a package public member in the main Class (Lab2TestDrive), something like
public class Lab2TestDrive{
...
String cale;
public static main void(String[] args){
JButton button1.. button2.. button3..
}
Can I implement an ActionListener on Lab2TestDrive, and override the actionPerformed(...) method in there? But if I do that, I don't know how I would be able to know which button triggered the actionPerformed method.
I know I could make a separate class,
public class ButtonListener implements ActionListener {
JButton button;
ButtonListener(JButton button){
this.button = button;
}
#Override
public void actionPerformed(ActionEvent arg0) {
if(button.getText().equals("Save")){
};
}
}
But then I don't know how I could access the "cale" variable :(
First: You should not let your ActionEvent be called arg0.
Then: You can technically do it all in one class. Your ActionEvent parameter has a method called getSource() which will get you the button that fired the event.
In theory, you could also create a third class storing your cale variable and give a reference to that class as a parameter to the constructor of your listener.
However, that seems very unintuitive.
You could also give a reference to your Lab2TestDrive object to the constructor of your Listener and then call a method from that class from within your actionPerformed.
Truth be told, none of those options really strikes me as great coding practice, but they should all work.

How do you add new functions to pre-existing java components?

To explain what I mean by this question I will use code examples below. Imagine you have this function.
private void fadeButton(JButton b, int timeToFade) {
//Fade code goes here
}
How would you implement this as a function which could be run like
JButton b = new JButton("Press Me");
b.fadeButton(20000);
Where fadeButton now looks like
private void fadeButton(int timeToFade) {
//Fade code goes here
}
Because the function is declared on the button itself.
Typically you create a derived class:
public JFadableButton extends JButton
This will contain the method private void fadeButton(int timeToFade).
Short answer is: you don't.
Longer answer:
You can't do that in Java directly (adding methods to a class outside of the source code of that class). That might be different in other languages, like Kotlin offers "something" like that.
In java, you have to make detours, for example by turning to the decorator pattern.
And just for the record: I didn't mention the simple "you can extend that class" because I read your question as "how do I add methods to JButton directly". But of course, creating your own class that extends JButton allows you to add methods; but of course, they only exist on objects of your derived class.
You could extend JButton with a new class, thus inheriting JButton's methods and adding the ability to add your own code:
public class FadingButton extends JButton {
//Constructors go here
private void fadeButton(int timeToFade) {
//Fade code goes here
}
}
You could also decorate the JButton with another class:
public class JButtonDecorator {
private JButton btn;
//Constructor here
private void fadeButton(int timeToFade) {
//Fade code goes here, hiding the held button
}
//getter and setter method for button
}
Or, if you want lots of different ways to affect your UI, you can make a utility class, similar to above:
//You could use a factory pattern to make this a singleton instead of having static methods
public abstract class UIUtils {
private UIUtils{} //Don't instantiate this class
public static void fadeComponent(JComponent toFade) {
//Fade code goes here
}
//Other static utility methods
}
Edit: Making use of these patterns. The extended class is self-explanatory and an example of simple inheritance, so it's just a matter of JButton btn = new FadingButton(); for example. Here are the others:
To use the decorator, instantiate it at the same scope as the button you're using now. For example:
JButton myButton = new JButton();
//Customize button and add to UI
JButtonDecorator jbDec = new JButtonDecorator(myButton);
jbDec.fadeButton(20000);
Although the button is a field of the decorator, it will otherwise behave normally in your UI. The decorator just wraps the class with useful methods such as the fadeButton method.
To use the utility class, there are two ways. One is two make an abstract class with static methods (as above), some consider it bad form but it's good for simple programs:
UIUtils.fadeComponent(myButton); //It's just that simple!
//The UIUtils class itself is never instantiated.
//All the methods are static, so no instances are needed.
Or if you want a more advanced method, make your utility class a singleton. This changes the utility class to this:
public class UIUtils {
UIUtils singleton;
private UIUtils{} //Don't instantiate this class publicly
public static UIUtils getInstance() {
if(singleton==null) //This is the first time the method is called
singleton = new UIUtils();
return singleton; //Return the one instance of UIUtils
}
public void fadeComponent(JComponent toFade) {
//Fade code goes here
}
//Other utility methods
}
Then you would declare your UIUtils object at class level to use across your UI:
UIUtils uiUtil = UIUtils.getInstance();
And somewhere in your code:
uiUtil.fadeComponent(myButton);
This pattern is more efficient with memory and is more object-oriented, but I don't personally find it very suitable for utility classes.
You can create a new class which extends JButton, and then add any method that could help you achieve what you want. But that's an exemple, there is many ways to achieve this.
Ps, don't set this method as private if you want to use it somewhere else than inside your class.
This is the simplest way I think think of. You have already got it. But just use this method:
private void fadeButton(int timeToFade) {
//Fade code goes here
}
This is assuming you already have the code for the fade, do you? I think this one is the one you should use. You don't need the button to be a parameter. When you want to call the method to fade the button, just put it in the ActionListener. So after you have the ActionListener for the button, do the following: btnName.fadeButton(timeToFade);
Here is how to code the ActionListener:
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
}

Syntax of adding an action listener

When I enter the following code:
JButton aButton = new JButton("Button");
aButtin.addActionListener(this);
my IDE (NetBeans) highlights this as a leaking this statement.
Though there never seem to be any negative repercussions from using the statement as such, some docs that I have read indicate that the more proper method is to add the action listener as a new instance of an inner class that implements ActionListener.
What do NetBeans and these docs know that I don't?
What is really meant by a leaking this statement?
Are there negative repercussions that I am unaware of in the use of this in this way?
there are three ways
aButton.addActionListener(this); in the case that class declarations contains implements ActionListener and there is public void actionPerformed(ActionEvent ae) { too
f.i. pseudocode
public class ButtonDemo implements ActionListener{
aButton.addActionListener(this);
#Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == aButton) {
}
}
}
aButton.addActionListener(new ButtonPress()); is in the case that ActionLIstener is declared as separate class
f.i. pseudocode
public class ButtonDemo {
aButton.addActionListener(new ButtonPress());
}
public class ButtonPress implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
}
}
simpler, clear and without side effects (a.m. two ways) is to create inner anonymous listener
f.i. pseudocode
aButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
If you use this, then in order for this to handle ActionEvents from multiple sources you need to add logic to check the source or command string of the action. This is perfectly reasonable.
If you use other classes, then you can use ActionListeners that are implemented elsewhere, or reuse ones that were designed for a specific common purpose, or define e.g. anonymous inner classes inline which may be convenient in your situation. This is also perfectly reasonable.
Do not think in terms of "advantages" or "disadvantages" -- this is such a common mistake (Is it "bad" to do xyz? Is xyz "good practice"?). You use whatever makes the most sense for your situation and provides the clearest, most maintainable, properly functioning code. Have common sense, be familiar with the language you are working in and the options available, make sane judgments. A language is a way to express an idea, speak (type) clearly.
I guess one disadvantage of using the inner class implementation is that the listener cannot be reused for other buttons , in case, they should have the same listener.
Something like this :
Can you use the same OnClickListener for different buttons?

What is the difference between addActionListener(this) and addActionListener(new ActionListener)?

I want to add event handling to buttons- and I noticed that there are two ways to do this.
Implement the ActionListener interface and then append event listeners to the buttons.
Example:
countButton.addActionListener(this);
And the in the ActionPerformed method will run to display the result.
Do not implement the ActionListener interface and instead do this:
countButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
}
});
How does the second method work exactly?????!!!
Thanks!
It is not necessary to define a second class for the first approach. You just need to add the
public void actionPerformed(ActionEvent e) method inside your class and do whatever you want there, after you make your class implement ActionListener. You could use a second class if you wanted to, but it is not necessary. The disadvantage is that you need to specify the source of the event with long if statements in order to take the appropriate action if you have more than one JButtons i.e.
The second approach is adding an anonymous inner ActionListener to every component. It is a more Object Oriented approach, since you have a clearer separation of the functionality of your widgets. It is of advantage to define an additional method called inside each actionPerformed, in order to be able to use this freely and refer to the containing class:
countButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doCountButtonAction(e);
// "this" here would refer to the ActionListener (not that useful)
}
});
and implement the method:
private void doCountButtonAction(ActionEvent e) {
// do whatever you need to here
// using "this" here refers to the containing class (i.e. JFrame, JPanel or whatever)
}
Your question is not exactly on listeners as it is on how interfaces work and how you can instantiate a class in Java. Here's some finer points:
Basicaly, what the JButton class offers you is a way to declare a class who's one particular method will be called when an event is triggered on the button, like when it is clicked. If you look at the Javadocs for JButton and ActionListener, you will now how they work:
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/AbstractButton.html#addActionListener(java.awt.event.ActionListener)
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html
What you can do here in the most old fashioned way possible is to create a class that will be triggered when someone clicks your button:
public class MyButtonActionListener implements ActionListener {
actionPerformed(ActionEvent e) {
System.out.println("Aww, you clicked!");
}
}
Now, once that's done, you can make an insance of it and add it as a listener to your button:
JButton button = new JButton("My button");
MyButtonActionListener myActionListener = new MyButtonActionListener ();
button.addActionListener(myActionListener);
On the other hand, in Java you can instantiate a class anonimousy, which means that instead of having a handler to it's instance (like myActionListener in the above code), you just instantiate it on the fly in the place you need it, and you'll have no handler to use it later. That's what's happening in your code: an ActionListener implementation is delcared on the fly as the parameter for the addActionListener method, that on the fly declaration also includes the statement that your anonymous instance is not just any class, but one that implements ActionListener, and such your anonymous declaration needs to give an implementation of the actionPerformed method.
A third option is to have a class that implements ActionListener (and the actionPerformed method), and inside that class, if you create a JButton and you want to pass it as listener an instance of the ecompasing class, you'll use "this" to reffer to that, as in :
public class MyButtonActionListener implements ActionListener {
private JButton button = new JButton();
public void init() {
button.addActionListener(this);
}
public actionPerformed(ActionEvent e) {
System.out.println("u clicked!");
}
}
There's alot of finer points to this discussion (as in how do you reffer to the "this" on a n anonymous class delcared inside an other class, and how do you reffer to the "this" of the encompassing class instance). I recommend that you read a book on the Sun Certified Java Programmer certification, it has a chapter dedicated to this
That just creates an anonymous class that implements the actionPerformed method. This way you don't have to define a new class for every place that you want to handle an event.
It works the same way as the first technique i.e. The method expects an object which implement ActionListener interface. However, both techniques has its own merits and demerits. The first technique allows utilizing single object on the expense of cluttered if/else code because same method will be handling events for multiple buttons. The second technique allows cleaner separation of logic for each button on the expense of creating multiple anonymous objects (for each button).
Either way, to add an ActionListener you need a class that implements the ActionListener interface (which defines a single method, actionPerformed). addActionListener(this) implies that this implements that interface, and any action performed will call this.actionPerformed.
In the second case, what you're doing is creating a new, anonymous, class that implements the ActionListener interface. When countButton is clicked, the actionPerformed method called is the one in the anonymous class.

Java Event Binding

I'm new in windows application development using java. The question is this "How to i bind events to custom class methods?
As fas i have seen i can register listener classes to swing components to handle events. That is OK but i have to implement a class that implements e.g. the ActionListener interface to handle an event and then implement the actionPerformed method. That means ONE class FOR each event i have to handle?
Can't i have ONE class "listening" for events from all components and have each method in this class do the event handling?"
An example:
class MyEventListener { //pseudo code [no constructors, etc]
public void handleSubmitFormBtn(Event e) {
}
//other methods go here handling events from other swing components
}
Note: I am not sure about the method signature but i hope that you get the point.
Conclusion: ONE method to handle events fired from swing components..is it possible? Is the creation of ONE class for each the event the only way? Can't i route event handling to specific methods for a single class?
In swing what you usually do is use an anonymous class to handle your events, like so:
someControl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// handle the event
}
});
Although you can basically have one class handling all your events, and register it to all handlers, anonymous classes, as mentioned above, are the correct swing idiom for handling events.
You can find much more info # http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html
You have a few choices, each with their particular benefits/drawbacks.
anonymous inner classes
component.addActionListener(
new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{
outerClassesMethod();
}
});
inner class
class Foo
implements ActionListener
{
public void actionPerformed(final ActionEvent e)
{
outerClassMethod();
}
}
outer class
public class Foo
implements ActionListener
{
private final OuterClass target;
public Foo(final OuterClass t)
{
target = t;
}
public void actionPerformed(final ActionEvent e)
{
target.targetClassMethod();
}
}
class implements listener
public class OuterClass
implements ActionListener
{
public void actionPerformed(final ActionEvent e)
{
method();
}
// somewhere else in the code
{
component.addActionListener(this);
}
}
Each way has good and bad to it.
The anonymous inner class will not allow you to do what you are asking, it can only implement one listener.
The other three will all allow you to do what you want (just add , WindowListener to the implements list for exaple). You likely want the inner class or outer class implementing the listener way to do what you want. I suggest that because the listener is likely very highly coupled to your program, and you will need to do a large set of "if" statements to figure out which control was acted on to perform the actual action (you use evt.getSource() to figure out which control was being acted on and then comare it to your instance variables to see which it was).
However, unless you are in memory constrained device, such as an Android phone, you probably should not do one method for all listeners as it can easily lead to very bad code. If memory is an issue, then go for it, but if it isn't you are better of doing one of the following things:
one listener class per control
one listener class per event type for all controls
one listener class per control per event type
I prefer to code the following way, I find it to be the most flexible:
public class Outer
extends JFrame
{
private final JButton buttonA;
private final JButton buttonB;
{
buttonA = new JButton("A");
buttonB = new JButton("B");
}
// do not put these in the constructor unless the Outer class is final
public void init()
{
buttonA.addActionListener(new AListener());
buttonB.addActionListener(new BListener());
}
private void aMethod()
{
}
private void bMethod()
{
}
public void AListener
implements ActionListener
{
public void actionPerformed(final ActionEvent evt)
{
aMethod();
}
}
public void BListener
implements ActionListener
{
public void actionPerformed(final ActionEvent evt)
{
bMethod();
}
}
}
I prefer this way because it forces the methods out of the listeners, which means I only have one place to look for the code (not scattered throughout the inner classes). It also means that it is possible that aMethod() and bMethod() can be reused - if the code is in a listener that isn't practical (it is possible, but not worth the effort).
Doing it the above way is also consistent, and I prefer consistency over most things unless there is a good reason not do it. For instance on the Android I do not do that since class creation is expensive (I still have the listener call methods only, but the class itself implements the listeners and I do an if statement).
You can do this by creating a single action listener that then switches based on the input source, so something like:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonA) {
doSomethingForButtonA(e);
} else if (e.getSource() == buttonB) {
doSomethingForButtonB(e);
}
}
But this is not the recommended way to do it for various reasons. Why do you have an issue with creating a listener for every event to be received? It's the Java model for handling UI events, and if anyone else uses your code or you ever use someone else code it's going to be expected.
You can have one class which has listener methods of many events:
class EventHandler implements ActionListener, ..., MouseListener {
// implementation
}
A conceptual Solution would be to implement the OBSERVER PATTERN.
Melasse framework allows to glue UI component with model w/o creating class (even anonymous one), with syntax Binder.bind(/* source, target, options */).
For example not new class is needed to enable an action/button only when some text is present in a textfield: https://github.com/cchantep/melasse/blob/master/README.md#bind-buttonaction-to-provided-value . Same to display/hide informational or error label.
Most UI components are supported, all Java Beans (with property change support) are.

Categories