In this tutorial you will learn about the Swing JButton and its application with practical example.
Swing JButton
The Jbutton which is implementation of push button is used to create labelled button. On pressing it generate an event. The JButton inherits AbstractButton and implements Accessible.
Declaration of JButton class
1 |
public class JButton extends AbstractionButton implements Accessible |
JButton Constructor
Some of commonly used constructor are listed below.
JButton()
This constructor is used to creates a button with no set text and icon.
Syntax:-
1 |
JButton jb=new JButton(); |
JButton(String text)
This constructor is used to creates a button with text.
Syntax:-
1 |
JButton jb=new JButton(“OK”); |
JButton(Icon i)
If you want to set an Icon on your button so this constructor is used.
Syntax:-
1 2 3 |
ImageIcon icon=createImageIcon(“/resources/icon.png”,”Java”); JButton jb=new JButton(icon); |
JButton(String text, Icon i)
This constructor is used to creates a button with initial text and an icon.
Syntax:-
1 2 |
ImageIcon icon=createImageIcon(“/resources/icon.png”,”Java”); JButton jb=new JButton(“OK”,icon); |
JButton(Action a)
This constructor is used to creates a button where properties are taken from the action supplied.
Syntax:-
1 2 3 4 5 6 7 |
jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Your Event on click of Button } }); |
Swing JButton Methods
Some of commonly used methods are listed below.
void setText(String text): This method is used to set Text on Button.
String getText(): This method is used to get Text of Button and return type is String.
void setEnabled(boolean b): This method is used to Enable and disable the Button.
void setIcon(Icon ic): This method is used to set icon on Button.
Icon getIcon(): This method is used to get Icon of Button.
void setMnemonic(int i): This method is used to set Mnemonic on the JButton.
AccessibleContext getAccessibleContext(): This method is used to get the AccessibleContext which is associated with JButton.
String getUIClassID(): This method specifies the name of the L&F class that renders this component and returns a String.
boolean isDefaultButton(): This method is used to get defaultButton property and returns boolean.
boolean isDefaultCapable: This method is used to get the value of the defaultCapable property and returns boolean.
protected String paramString(): This method is used to returns a string representation of this JButton.
void updateUI(): This method is used to resets the UI property.
JButton Inherited Methods
This class inherits methods from the following class:
- javax.swing.AbstractionButton.
2. javax.swing.JComponent.
3. java.awt.Container.
4. java.awt.Component.
5. java.lang.Object.
Lets understand it by an example. First we need to create a class like below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package w3addaswing; import javax.swing.JButton; import javax.swing.JFrame; public class W3ADDASwing { public static void main(String[] args) { // TODO code application logic here JFrame f = new JFrame("Button Example"); JButton b = new JButton("Click"); b.setBounds(130, 100, 100, 30); f.add(b); f.setSize(400, 400); f.setLayout(null); f.setVisible(true); } } |
The output of this code will look like below Image
Example of JButton with ActionListener : Lets write code for ActionListener of JButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package w3addaswing; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class W3ADDASwing { public static void main(String[] args) { // TODO code application logic here JFrame jf = new JFrame("Button Example"); final JTextField tf = new JTextField(); tf.setBounds(50, 100, 150, 20); JButton jb = new JButton("Click"); jb.setBounds(50, 50, 95, 30); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tf.setText("Welcome to W3Adda.com"); } }); jf.add(jb); jf.add(tf); jf.setSize(400, 400); jf.setLayout(null); jf.setVisible(true); } } |
The Output of above example will look like below image
Example of JButton with Icon: Lets do coding for displaying icon on button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package w3addaswing; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class W3ADDASwing { public static void main(String[] args) { // TODO code application logic here JFrame jf = new JFrame("Button Example"); final JTextField tf = new JTextField(); tf.setBounds(50, 100, 150, 20); JButton jb = new JButton(new ImageIcon("D:\\eye.png")); jb.setBounds(50, 50, 95, 30); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tf.setText("Welcome to W3Adda.com"); } }); jf.add(jb); jf.add(tf); jf.setSize(400, 400); jf.setLayout(null); jf.setVisible(true); } } |
Output of this code will look like below image