Java-Swing:Introduction to Swing - Introduction to JButton

From Juneday education
Jump to: navigation, search


Screenshot of a window with two buttons

A JButton is a widget representing something you can click. It can have text describing its purpose, or an icon. You add a listener object (or more) to a button. The listener object represents the action to take when the button is clicked by the user.

By separating the code for the action from the button, we maintain separation of concerns, and we'll get cleaner code.

When the button is clicked, the GUI system makes sure a certain method is called on the listener object. The method is defined in the interface type of the listener, the ActionListener interface, and the listener object is an object implementing that interface and the only required method public void actionPerformed(ActionEvent ae).

Teaching materials



Further reading

Where to go next

« PreviousBook TOCNext »