Share on Google+Share on Google+

Java Swing : JButton Example

In this section we will discuss how to create button by using JButton in swing framework.

Java Swing : JButton Example

In this section we will discuss how to create button by using JButton in swing framework.

JButton :

JButton Class extends AbstractButton class and implements Accessible interface. It is an implementation of a "push" button. It is similar to the simple button.
You can create JButton labeling with some String .Some events are handled with the button. For that you can attach an ActionListener by using
addActionListener() method.

Methods : Following are JButton methods -

getAccessibleContext() : this method provides you the AccessibleContext associated with specified JButton.

getUIClassID() : Return type is String. It specifies the name of the L&F class that renders this component.

isDefaultButton() : It gets the value of the defaultButton property and returns true if button is the current default button for its JRootPane.

isDefaultCapable() : It gets the value of the defaultCapable property.

paramString() : It returns a String representation of the JButton.

removeNotify() : This method overrides JComponent.It checks if specified button is currently set as the default button on the RootPane,
then it sets the RootPane's default button to null to ensure the RootPane doesn't hold onto an invalid button reference.

setDefaultCapable(boolean defaultCapable) : It sets the defaultCapable property which checks the capability of your button that can be made default button for its root pane or not.

updateUI() : It resets the UI property to a value according to present look and feel.

Example :

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

public class CreateJButton{

public static void main(String[] args) throws Exception{
JFrame f = new JFrame("JButton Example");
f.setLayout(new FlowLayout());
JButton button=new JButton("Click");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Welcome To Roseindia Technologies");

Description: JFrame creates a new initially invisible frame with your given title. It sets the LayoutManager and conditionally overrides setLayout() in container.
FlowLayout() constructs a new FlowLayout with centered alignment and a default 5-unit horizontal and vertical gap.
Next you can create object of Button as - JButton button=new JButton("Click");
addActionListener() method adds an ActionListener to the button. JOptionPane creates a standard dialog box that provoke users for a value or some information. add() method adds the mentioned component to the end of the container.
f.setSize(300,200) method resizes component f according to the specified width 300 and height 200.
f.setVisible(true) shows the Window f as value of parameter is true. If you specify false then it will hide the window.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) sets the operation at the time user close the current frame. JFrame.EXIT_ON_CLOSE uses System exit method and exit the application.

Output :



Posted on: September 10, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.