beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Swing – JButton tutorial and examples

By Chaitanya Singh | Filed Under: Swing

JButton class is used for adding platform independent buttons to a swing application. In this tutorial we will learn how to create a button in Swing application and how to tweak their appearance as per the requirement. I have also shared some code snippets that may be useful for you while developing a Swing application.

Swing JButton Example

This example shows how to create a button in a Swing application and how to add it to a frame. Along with this, we will see various methods of JButton class.

import javax.swing.JButton;  
import javax.swing.JFrame;  
public class JButtonExample {
    JButtonExample(){  
        /* JFrame is a top level container (window)
         * where we would be adding our button
         */
        JFrame frame=new JFrame();  
                          
        // Creating Button          
        JButton b=new JButton("Click Me..");
        /* This method specifies the location and size
         * of button. In method setBounds(x, y, width, height)
         * x,y) are cordinates from the top left 
         * corner and remaining two arguments are the width
         * and height of the button.
         */
        b.setBounds(50,50,90, 50);  
             
        //Adding button onto the frame
        frame.add(b);  
                  
        // Setting Frame size. This is the window size
        frame.setSize(300,200);  
        
        frame.setLayout(null);  
        frame.setVisible(true);  
                  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                  
    }  
              
     public static void main(String[] args) {  
            new JButtonExample();  
     }  
}

Output:

Jbutton example - Swing

Some Useful JButton examples

You would find some useful code snippets in this section that you can use in your projects.

1) Adding image to JButton

The simplest way is this:

JButton b=new JButton(new ImageIcon("imageName.png"));

The above statement would work if you have the image in the folder where your Swing program files are stored.
However if you have image in some other folder then do like this:
For example if you have programs in “src” folder and image in “src/resources” folder then code it like this: This is the recommended way of adding image/icons to your buttons as this will reduce the chances of any ambiguities further.

JButton button = new JButton();
try {
    Image img = ImageIO.read(getClass().getResource("resources/myimage.png"));
    button.setIcon(new ImageIcon(img));
} catch (IOException ex) {
  }

Here is the complete code: Here I have a image play.gif placed in the folder where my program files are.

import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class JButtonIcon {
    JButtonIcon(){  
        /* JFrame is a top level container (window)
         * where we would be adding our button
         */
        JFrame frame=new JFrame();  
                          
        // Creating Button          
        JButton b = new JButton();
        try {
            Image img = ImageIO.read(getClass().getResource("play.gif"));
            b.setIcon(new ImageIcon(img));
          } catch (IOException ex) {
          }
        
        b.setBounds(50,50,90, 50);  
             
        //Adding button onto the frame
        frame.add(b);  
                  
        // Setting Frame size. This is the window size
        frame.setSize(300,200);  
        
        frame.setLayout(null);  
        frame.setVisible(true);  
                  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                  
    }  
              
    public static void main(String[] args) {  
            new JButtonIcon();  
    }  
}

Output:

Adding image/icon to JButton example

Enjoyed this post? Try these related posts

  1. Swing – BorderLayout in Java
  2. Java Swing Tutorial for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap