Run the program using the following command. This is called firing the event. The listener interface for receiving action events. Method Purpose Returns the string associated with this action. In such cases, this dividing into two partscan be a great help. The listener then performs some action. When the action event occurs, that object's actionPerformed method is invoked.
The size is set, and thecontent pane is obtained, with getContentPane as in previous examples. Gotcha: Changing the Parameter List for actionPerformed When you make a class into an action listener, the header for the method actionPerformed is determined for you. If you press either of the two color changebuttons nothing will happen, but you can use this phase of coding to adjustdetails, such as which button is listed first. But buttonGui is the action listener for the buttons in buttonGui,so the action event goes to buttonGui. My Java AbstractAction demo class Now that you've seen the background information, here's the source code for a complete Java class which shows several custom actions which extend the Java AbstractAction class: package action; import java. Java ActionListener Interface The Java ActionListener is notified whenever you click on the button or menu item. Provide details and share your research! A button fires events known as action events , which are handled by listeners known as action listeners.
The state that an action can handle includes text, icon, mnemonic, enabled, and selected status. It is justthere so that we would get a warning, if we made some unnoticed mistake in thecode. To add interactivity in a program, Java provides us a very easy way. The following line from Display 12 stopButton. The only action they took was to go awayin response to clicking the close-window button.
Different kinds of components require different kinds of listener classes tohandle the events they fire. . The Java Programming Forums are a community of Java programmers from all around the World. To end the program and make the windowdisappear, you click the close-window button. } We could have defined a separate class that did nothing but handle buttonevents, but it's more convenient to make the window class ButtonDemo into the Action-Listener that will handle buttonevents. An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields.
Buttons and Action Listeners I claim not to have controlled events, but confess plainly that eventshave controlled me. If e is an action event that wasfired by clicking a button, then e. Here, you will defined what will your program do when the user click the component being monitored for an action event. Welcome to the Java Programming Forums The professional, friendly Java community. We will discuss buttons andthe new listener class in the next few subsections.
It has only one method: actionPerformed. For example, consider the program in Display 12. The default action command is the string written onthe button, but if you want to, you can specify that a different string be theaction command for a button. Example Where Described Notes This section and Contains one button with one action listener that beeps when you click the button. The ActionListener interface is found in java. SwingListenerDemo Verify the following output. Let's look at the code for the method actionPerformed in the class ButtonDemo in Display 12.
You create buttons in a way that is very similar to how you create labels. The button is added to the content pane with the following: contentPane. If you click one of the buttons inside of buttonGui with your mouse,that sends an action event to the action listener for that button. In the next subsection we explain the lines from Display 12 involving themethod addActionListener. In this tutorial you will learn: 1. Awindow listener of the class WindowDestroyer is added with the method addWindowListener as in previous examples. Let's continue with our object buttonGui of the class ButtonDemo.
Recall that within thedefinition of a class, an object of that class is called this. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. You can find the entire program in. Scripting on this page tracks web page traffic, but does not change the content in any way. You would do soas follows: stopButton. The other example described in that section, , has two action sources and two action listeners, with one listener listening to both sources and the other listening to just one.