Introduction to Java Swing Button
Button is a component where click event gets triggered every time the user clicks and the corresponding method is called Java Swing Button. It is the most commonly used component for triggering events or actions and submitting forms in java swing application. In Java button class uses JButton abstract class. This abstract class AbstractButton extends the JComponent class. Many types of a button can be instantiated by using classes descending from AbstractButton. Different types of buttons which are subclasses of AbstractButton are as follows:
- Common Button: It is created using class JButton and the type of event triggered by this button is ActionEvent.
- ToggleButton: It is used to toggle between true or false values.
- CheckBox: It is used to generate an event called ItemEvent.
- RadioButton: It is also used to generate an event called ItemEvent.
How class is declared for javax.swing.JButton :
class JButton extends AbstractButton implements Accessible{}
Constructors and Methods of Java Swing Button
Here we will discuss the constructors and methods of java swing button:
Constructors
Constructors are used for instantiating objects for JButton class:
- JButton(): Creates and instantiates a button having no set text or icon.
- JButton(Event e): Creates and instantiates a button with the event supplied and properties are also taken from action supplied.
- JButton(String s): Creates and instantiates a button with text with string s as provided.
- JButton(Icon i): Creates and instantiates a button with an icon i as provided.
- JButton(String s, Icon i): Creates and instantiates a button with initial text as a string and an icon provided respectively.
Methods
Common methods used for JButton:
- void setText(String s): Sets the provided text to a button created.
- String getText(): Gets the text on the button.
- void setAction(Action a): Sets the button properties according to the action provided.
- Action getAction(): Gets the button properties with respect to action presented in it.
- void setIcon(Icon i): Sets the icon or image displayed by the button when it is not pressed. To set an icon on a pressed button use void setPressedIcon(Icon i)
- Icon getIcon(): Gets the icon displayed on the button when it is not pressed or selected. To get an icon from a pressed or selected button use Icon getPressedIcon().
- void addActionListener(ActionListener a): Adds the objects that listens to the action events generated by the button.
- ActionListener removeActionListener(): Removes the objects that listens to the action events generated by the button.
- void addActionListener(ActionListener a): Adds the objects that listenes to the action events generated by the button.
- ActionListener removeActionListener(): Removes the objects that listenes to the action events generated by the button. Sets the objects that listenes to the actions generated by the button. Fine Tuning the JButton appearance:
- void addItemListener(ItemListener a): Adds the objects that listenes to the item events generated by the button.
- ItemListener removeItemListener(): Removes the objects that listenes to the item events generated by the button.
- void setSelected(boolean): Set the button in selected mode or pressed mode.
- boolean isSelected(): Gets the value of button whether it is selected or not AbstractButton provides methods to align the position of an icon and also the position of text on button
- void setHorizontalAlignment(int alignment): Sets the horizontal alignment of icon and text. AbstractButton’s default is SwingConstants.CENTER. Some of the other alignments that can be provided:
- SwingConstants.RIGHT
- SwingConstants.LEFT
- SwingConstants.LEADING
- SwingConstants.TRAILING
- void setVerticalAlignment(int alignment):
- Sets the vertical alignment of icon and text.
- Some of the other alignments that can be provided:
- SwingConstants.TOP
- SwingConstants.BOTTOM
- SwingConstants.CENTER(default)
- int getHorizontalAlignment(): It returns the horizontal alignment of icon and text.
- int getVerticalAlignment(): It returns the vertical alignment of icon and text.
void setHorizontalTextPosition(int) and void setVerticalTextPosition(int): Used to set horizontal and vertical position of button. For horizontal the positions are: RIGHT, LEFT, LEADING, TRAILING(default), CENTER. For vertical the positions are: TOP, CENTER (the default), and BOTTOM.
- int getHorizontalTextPosition() and int getVerticalTextPosition(): Used to get horizontal and vertical position of button.
Examples of Java Swing Button
Following are the examples as given below:
Example #1 – Simple JButton Code
Code:
import java.awt.*;
import javax.swing.*;
public class Main {
Main(){
JFrame newFrame=new JFrame("eduCBA");
// Creating Button
JButton b=new JButton("Click");
b.setPreferredSize(new Dimension(100,30));
newFrame.add(b);
newFrame.setSize(250,250);
newFrame.setLayout(new FlowLayout());
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Main();
}
}
Output:
Example #2 – Adding an image to the button
Code:
import java.awt.*;
import javax.swing.*;
public class JButtonExample {
public static void main(String[] args) {
JFrame frame=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("C:\\custom.png"));
frame.add(b);
frame.setSize(500,500);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
Output:
Recommended Articles
This is a guide to Java Swing Button. Here we discuss the constructors and methods of the java swing button along with different examples and its code implementation. You may also look at the following articles to learn more –