Study Anytime Any Where We Will Open The Knowledge For You Anish Sir 24x7 Learning solution It Is Time to join JOB
22 August 2017

Study Anytime Any Where

22 August 2017

We Will Open The Knowledge For You

22 August 2017

Anish Sir 24x7 Learning solution

22 August 2017

It Is Time to join JOB

C Programming

C++ Programming

JAVA Programming

Android Language

Android-logo.png

.Net Programming

net-logo-b55c217213ae49991589ed43d7ed58a7.png

c# Programming

photo_1389636992_quiz_image_temp.png

Asp .Net

asplogo-square.png

PHP Programming

58481791cef1014c0b5e4994.png

Search Your Topic

config
Saturday, 05 August 2017 08:10

Java JButton

Written by
Rate this item
(0 votes)

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.

JButton class declaration

Let's see the declaration for javax.swing.JButton class.

 
  1. public class JButton extends AbstractButton implements Accessible  

Commonly used Constructors:

Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon object.

Commonly used Methods of AbstractButton class:

Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener(ActionListener a) It is used to add the action listener to this object.

Java JButton Example

 
  1. import javax.swing.*;    
  2. public class ButtonExample {  
  3. public static void main(String[] args) {  
  4.     JFrame f=new JFrame("Button Example");  
  5.     JButton b=new JButton("Click Here");  
  6.     b.setBounds(50,100,95,30);  
  7.     f.add(b);  
  8.     f.setSize(400,400);  
  9.     f.setLayout(null);  
  10.     f.setVisible(true);   
  11. }  
  12. }  

Output:

JAVA Jbutton 1


Java JButton Example with ActionListener

 
  1. import java.awt.event.*;  
  2. import javax.swing.*;    
  3. public class ButtonExample {  
  4. public static void main(String[] args) {  
  5.     JFrame f=new JFrame("Button Example");  
  6.     final JTextField tf=new JTextField();  
  7.     tf.setBounds(50,50150,20);  
  8.     JButton b=new JButton("Click Here");  
  9.     b.setBounds(50,100,95,30);  
  10.     b.addActionListener(new ActionListener(){  
  11. public void actionPerformed(ActionEvent e){  
  12.             tf.setText("Welcome to Javatpoint.");  
  13.         }  
  14.     });  
  15.     f.add(b);f.add(tf);  
  16.     f.setSize(400,400);  
  17.     f.setLayout(null);  
  18.     f.setVisible(true);   
  19. }  
  20. }  

Output:

JAVA Jbutton 2


Example of displaying image on the button:

 
  1. import javax.swing.*;      
  2. public class ButtonExample{    
  3. ButtonExample(){    
  4. JFrame f=new JFrame("Button Example");            
  5. JButton b=new JButton(new ImageIcon("D:\\icon.png"));    
  6. b.setBounds(100,100,10040);    
  7. f.add(b);    
  8. f.setSize(300,400);    
  9. f.setLayout(null);    
  10. f.setVisible(true);    
  11. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  12.     }         
  13. public static void main(String[] args) {    
  14.     new ButtonExample();    
  15. }    
  16. }    

Output:

JAVA Jbutton 3

Read 1307 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.