< Prev
Next >



JButton class




JButton class is used to create a push button control, which can generate an ActionEvent when it is clicked. In order to handle a button click event, ActionListener interface should be implemented. JButton is a component which extends JComponent class and it can be added to the container.




Constructors of JButton


Constructor Description
public JButton() Creates a button with no text on it..
public JButton(String text) Creates a button with a text on it.
public JButton(Icon image) Creates a button with an icon on it.
public JButton(String text, Icon image) Creates a button with a text and an icon on it.





Methods of JButton class


Methods Description
public void setText(String text) Sets a String message on the JButton.
public String getText() Gets a String message of JButton.
public void setIcon(Icon icon) Sets an icon or image over the JButton.
public Icon getIcon() Gets the icon or image of the JButton.
void setHorizontalTextPosition(int textPosition) Sets the button message on the LEFT/RIGHT of its icon or image .
void setVerticalTextPosition(int textPosition) Sets the button message on the TOP/BOTTOM of its icon or image.





An example of JButton


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



public class Btn1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method

}//Closing the class A


class A //implements ActionListener
{
JFrame jf;
JButton button1, button2, button3;


A()
{
jf= new JFrame();
button1= new JButton();
button2= new JButton("Click here");
button3= new JButton();

button3.setText("Button3");



jf.add(button1);
jf.add(button2);
jf.add(button3);

jf.setLayout(new FlowLayout());
jf.setSize(300,100);
jf.setVisible(true);
}

}

When you run the code, you are presented a window shown below -:

Figure 1





Setting an image over JButton


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;



public class Btn4
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A 
{
JFrame jf;

JButton button1, button2, button3, button4;

A()
{
jf= new JFrame();

//Setting an image over a JButton and limited its border space, so that image fits perfectly over JButton.
button1= new JButton(new ImageIcon("JButton1.jpg"));
button1.setBorder(new EmptyBorder(2,2,2,2));

//Setting an image over a JButton with and without text.
button2 = new JButton(new ImageIcon("Smiley3.jpg"));
button3 = new JButton("Smiley", new ImageIcon("Smiley3.jpg"));

//Setting a big image over a JButton
button4 = new JButton(new ImageIcon("Big Smiley", "BigSmiley.gif"));
button4.setHorizontalTextPosition(SwingConstants.LEFT);	

jf.add(button1);
jf.add(button2);
jf.add(button3);
jf.add(button4);


jf.setLayout(new FlowLayout());
jf.setSize(300,300);
jf.setVisible(true);
}

}
When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2





Handling button click events by implementing ActionListener interface


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


public class Btn5
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A implements ActionListener
{
JFrame jf;
JButton button1, button2;
JLabel label;


A()
{
jf= new JFrame();
button1= new JButton("Yes");
button2= new JButton("No");
label = new JLabel();


jf.add(button1);
jf.add(button2);
jf.add(label);


button1.addActionListener(this);
button2.addActionListener(this);


jf.setLayout(new FlowLayout(FlowLayout.CENTER,60,10));
jf.setSize(250,150);
jf.setVisible(true);
}


public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Yes"))
{
label.setText("You've clicked Yes");
jf.add(label);
jf.setVisible(true);
System.out.println("Yes button is clicked");
}

if(ae.getActionCommand().equals("No"))
{
label.setText("You've clicked No");
jf.add(label);
jf.setVisible(true);
System.out.println("No button is clicked");
}
}

}
When you run the code, you are presented a window shown in the Figure3 below -:

Figure 3


When you click on any of the buttons, you will be displayed an appropriate message, for example, when we click on Yes button, we get such message -

Figure 4




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< JTextArea
JCheckBox >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2