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:
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:
Leave a Reply