Hello Friends, Welcome to another tutorial. In this tutorial, we will learn about JButton in Java Swing. In my previous tutorials, we have learned about JFrame, JLabel and JTextField classes of Java swing, and now we will be learning about another interesting component: JButton.
So let’s get started to JButton in Java Swing Tutorial,
JButton in Java Swing
What is JButton in Java ??
- It is a class in package javax.swing that is used to display a button on the screen .
- Basically it provides the functionality of a push button.
- It allows an icon,a string or both to be associated with the push button.
- Whenever the button is pressed an ActionEvent is generated.
- It’s three constructors are :-
- JButton(String str )
- JButton(Icon icon )
- JButton(String str,Icon icon )
So this was the brief description of JButton class in Java Swing and now we will know how to create a JButton.
Creating  a JButton in Java Swing
- For creating a JButton we will create the object of the JButton class and we will pass a string ( within quotes ) to it’s constructor.Example- JButton btn=new JButton (“click me “);
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); //creating object of JButton class and passing String to it's constructor JButton btn=new JButton("click me "); //Setting the location and size of the JButton btn.setBounds(50,50,100,50); //adding JButton to the frame's content pane f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
Changing Text of The JButton Dynamically
- We can change the text of the JButton dynamically(explicitly) at any time using the setText() method .
- We just have to call the setText() method using the object of the JButton class and have to pass any text(within quotes) as an argument .
- The previous text written will be overriden by the new text.
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JButton btn=new JButton("click me "); btn.setBounds(50,50,100,50); //changing text of JButton dynamically using setText() method btn.setText("mybutton"); f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output  :
- As we can see in the above image we have changed the text of JButton and the previous written text in constructor is overriden by the new text.
Setting Image In JButton
- For setting image in JButton we will follow the same exact steps as we have followed while we were setting image in JLabel.
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); //Creating object of ImageIcon class ImageIcon icon=new ImageIcon("java.png"); JButton btn=new JButton(icon); //setting the size and location of the JButton Image btn.setBounds(50,50,icon.getIconWidth(),icon.getIconHeight()); //adding JButton to the frame's content pane f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output:
Setting the Font Style And Font Size of JButton Text
- The process is same as we did when we were dealing with JLabel.
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JButton btn=new JButton("myButton"); btn.setBounds(50,50,300,100); //creating object of the Font class Font fo=new Font("Arial",Font.ITALIC,20); //calling setFont() method btn.setFont(fo); f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output  :
Setting the Font Color of JButton
- For setting the Font Color of JButton  we will use a method setForeground() method and we will follow the same exact process while we were dealing with setting the Font color of JTextField.
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JButton btn=new JButton("myButton"); btn.setBounds(50,50,100,100); //calling setForeground() method btn.setForeground(Color.RED); f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output  :
Setting the Background Color of JButton
- For setting the background color of JButton we will use the setBackground() method.
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JButton btn=new JButton("myButton"); btn.setBounds(50,50,100,100); btn.setForeground(Color.RED); //calling the setBackground() method btn.setBackground(Color.YELLOW); f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output  :
Setting Different Type of Cursor for a JButton
- Now we will see how to set different type of cursor for a JButton.
- For this we need to create an object of the Cursor class and in it’s constructor we will pass a cursor constant which is already defined in the Cursor class.(Example-Cursor.HAND_CURSOR).
- Now for applying it into JButton we will call a method setCursor()Â Â using the object of the JButton class and in its argument we will pass the object of Cursor class .
- Programming code is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
mport javax.swing.*; import java.awt.*; public class myJButton { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myJButton"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JButton btn=new JButton("myButton"); btn.setBounds(50,50,100,100); btn.setForeground(Color.RED); btn.setBackground(Color.YELLOW); //creating object of the cursor class Cursor cur=new Cursor(Cursor.HAND_CURSOR); //calling the setCursor() method btn.setCursor(cur); f.add(btn); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- As we can see in the above image we have changed the cursor (HAND_CURSOR). We can also use different type of cursors which is defined in the cursor class.
So guys Let me know if you are having any difficulties while understanding this JButton in Java Swing tutorial.
Nice work bhai.. Carry on
Easyprogram4u
Thaku bro 🙂
There is definately a lot to learn about this subject.
I like all of the points you’ve made. http://bing.co.uk
Thank You 🙂
Hello my name is Joselyn and I just wanted to send you a quick message here instead of calling you. I discovered your Getting Started With Javax Swing JButton Tutorial For Beginners website and noticed you could have a lot more hits. I have found that the key to running a successful website is making sure the visitors you are getting are interested in your website topic. There is a company that you can get keyword targeted traffic from and they let you try the service for free for 7 days. I managed to get over 300 targeted visitors to day to my website. http://hw23.de/04oz1 – Unsubscribe here: http://aici.cf/3
Hello,
I liked your work very much,
the way you describe every step , it is very helpful.
It helped me to understand many basic points.
I request you to upload more tutorials frequently Please.
Just put up more Tutorial for swing and GUI applicaiton and database connectivity.
you can also share jsp and servlet tutorial.
Thank You so Much
Stay Connected
nice explanation sir add more topic of object oriented programming through java