JButton in Java Swing Tutorial

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 JFrameJLabel 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 :
  • Output :
JButton in Java Swing
JButton in Java Swing-fig-1

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 :
  • Output  :
JButton in Java Swing
JButton in Java Swing-fig-2
  • 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 :
  • Output:
JButton in Java Swing
JButton in Java Swing-fig-3

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 :
  • Output  :
JButton in Java Swing
JButton in Java Swing-fig-4

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 :
  • Output  :
JButton in Java Swing
JButton in Java Swing-fig-5

Setting the Background Color of JButton

  • For setting the background color of JButton we will use the setBackground() method.
  • Programming code is given below :
  • Output  :
JButton in Java Swing
JButton in Java Swing-fig-6

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 :
  • Output :
Javax Swing JButton-fig-7
Javax Swing JButton-fig-7
  • 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.

8 thoughts on “JButton in Java Swing Tutorial”

  1. 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

    Reply
  2. 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.

    Reply

Leave a Comment