It’s very easy to create a JButton, as all you have to do is:
- Create a class tha extends JFrame.
- Create the JButons you want.
- Use add method to add JButtons to the frame.
- import javax. swing. ...
- button = new JButton(“Click Me!”) – Create a new instance of JButton with label Click Me!
- button. addActionListener(this) – Adds action when the user clicks the button.
- frame. ...
- frame. ...
- public void actionPerformed(ActionEvent e) – the method that will be called when the user clicks the button.
What is a jbutton in Java?
JButton class in Java is used to create push buttons that can be used to perform any ActionEvent whenever it is clicked. In Order to achieve event action, the ActionListener interface needs to be implemented. The Buttons component in Swing is similar to that of the AWT button component except that it can contain text, image or both. The JButton class extends the JComponent class and can be used in a container.
What are the types of constructors in JButton?
Types of Constructors Used in JButton Class. There are five types of constructors basically used in JButton Class: 1. JButton () This constructor will create a button in the component with no icon or text.
What is the method to return the icon of the specified button?
Icon getIcon ( ): This method is used to return the icon of the specified button.
5. Setting a JButton as the default button
A window can have a default button whose action will be invoked when the user hits Enter key. Here is the code to set the button as default button in the frame window:
7. JButton demo program
For reference, we created a demo program which incorporates all the practices mentioned above. The program looks like this: You can download source code of this program in the attachment section.
About the Author
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
What is addactionListener in Java?
addActionListener () method registers the ActionListener to the Java Button and the behaviour we want in response to the action is coded inside the “actionPerformed ()“ method. The actionPerformed () method is called just after the user executes any action.
Why import java.awt.event?
In this program, we need to import another new package java.awt.event because we are dealing with event handling and this package provides classes and interfaces that are used for event handling in awt and Swing.
What color is the Jframe background?
Now we can see that as soon as we click on the button, the background color of the JFrame has been changed to pink.
What is awt.event in Java?
Package java.awt.event is used for defining interfaces and classed that is used for event handling in AWT and SWING.
What are some examples of events in Java?
Basically they are associated with a component. Some examples of the events are pressing a button, entering a character via keyboard, selecting an item in a list , clicking the mouse etc. Events are supported by number of packages including java.util, java.awt, java.awt.event .
How many methods are there in EventObject?
EventObject contains two methods. getSource () and toString ().
