Creating Buttons with javax.swing.JButton Class

This section provides a tutorial example on how to create a button with the javax.swing.JButton class.

Buttons are so easy to create with the javax.swing.JButton class. Here is a sample program:

 * Copyright (c) 2014,, All Rights Reserved.
import java.awt.*;
import javax.swing.*;
public class JButtonOk {
   public static void main(String[] a) {
      JFrame f = new JFrame("My First Button");
      JButton b = new JButton("OK");

If you run this example, you will get:
JButton Text Button

Note: Clicking the OK button will trigger no actions, because no event listeners are added to the button component.

Sample programs listed in this section have been tested with JDK 1.3.1 to 1.8.0.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

JButton - Swing Button Class

Creating Buttons with javax.swing.JButton Class

 Creating Image Buttons with javax.swing.JButton Class

 Button Action Handler at the Component Level

 Button Action Handler at the Frame Level

 Mouse Click Handler at the Frame Level

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker


 PDF Printing Version