Java


Java Tutorial


Admission Enquiry Form

  

Java Swing JButton

Java Swing JButton

The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class. Swing buttons are embodied by the JButton class.A Swing button can feature a text label,an icon label, or a combination of both.JButton has platform independent implementation.

Constructors used with JButton

Type of Constructor Constructor Description
JButton()This constructor creates a button with no icon and text.
JButton(String)A button with the specified text.
JButton(Icon)A button with the specified icon.
JButton(String,Icon)A button with the specified text and icon.



The following example creates a button with Click Me as the the text on the button.

Example

JButton btnClick=new JButton("Click Me");

Basic Methods of JButton class :

Method NameDescription
String getText()This method returns the text of the button.
void setText(String str)This method is used to set specified text on button
void setIcon(Icon ic)This method is used to set the specific Icon on the button.
Icon getIcon()This method is used to get the Icon of the button.
void setEnabled(boolean bl)This method is used to enable or disable the button.
void setMnemonic(int a)This method is used to set the mnemonic on the button.
void addActionListener(ActionListener ae)This method is used to add the action listener to this object.




Example1 of JButton with JButton() and JButton(String) Constructors

import javax.swing.*;
class ButtonEx extends JFrame
{
JButton btn1,btn2 ;
public ButtonEx() {
btn1 = new JButton("Click Me");
btn2 = new JButton();
add(btn1);
add(btn2);
setLayout(null);
setSize(250,200);
btn1.setBounds(20,20,200,30);
btn2.setBounds(20, 60, 200, 30);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class ButtonExample1
{
public static void main(String[] args)
{
ButtonEx btnex=new ButtonEx();
}

}



Output:

The code above will display :



Example2 of JButton with JButton(Icon) Constructor

     

import javax.swing.*;
class ButtonEx  extends JFrame
{
JButton btn1 ;
Icon ic;
public ButtonEx() {
ic= new ImageIcon("D:\\compuhelp-website\\images\\COMPUHELP.PNG");
btn1 = new JButton(ic);
add(btn1);
add(btn1);
setLayout(null);
setSize(250,200);
btn1.setBounds(20,20,200,50);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class ButtonExample1
{
public static void main(String[] args)
{
ButtonEx btnex=new ButtonEx();
}
}



Output:

The code above will display :



Example of JButton with setMnemonic() and setToolTipText()

import javax.swing.*;
import java.awt.event.KeyEvent;

class ButtonEx extends JFrame
{
JButton btn1 ;
Icon ic;
public ButtonEx() {
btn1 = new JButton("Press");
btn1.setToolTipText("Press P with Alt key to check the setMnemonic
with keyevent");
add(btn1);
btn1.setMnemonic(KeyEvent.VK_P);
setLayout(null);
setSize(250,200);
btn1.setBounds(20,20,200,30);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class ButtonExample1
{
public static void main(String[] args)
{
ButtonEx btnex=new ButtonEx();
}

}



Output:

The code above will display :



Example of JButton with setEnabled()

import javax.swing.*;
class ButtonEx  extends JFrame
{
JButton btn1 ;
public ButtonEx() {
btn1 = new JButton("Welcome to Compuhelp");
btn1.setEnabled(false);
add(btn1);
setLayout(null);
setSize(250,200);
btn1.setBounds(20,20,200,50);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class ButtonExample1
{
public static void main(String[] args)
{
ButtonEx btnex=new ButtonEx();
}
}



Output:

The code above will display :



Example of Java Swing JButton with ActionListener

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

class SwingButton extends JFrame implements ActionListener
{
JButton btn ;
public SwingButton() {
btn = new JButton("Click Me");
btn.setToolTipText("Click on button");
btn.addActionListener(this);
btn.setBounds(20,50,150,30);
add(btn);
setSize(250,200);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent ae)
{

btn.setText("Welcome to Compuhelp");
} }
public class ButtonExample
{
public static void main(String[] args)
{
SwingButton sbtn=new SwingButton();
}
}



Output:

The above code will display :