How to create Java JButton swing component

How to create Java JButton Swing Component

Its pretty easy to create Java JButton swing component.All you need to do is first to import it “import javax.swing.JButton” then go ahead using available JButton constructors to create the class.

Java JButton  Constructors

Here are some of the commonly used JButton constructors.

  • JButton(String s)-creates a JButton with a text.
  • JButton()-creates empty JButton.
  • JButton(Icon icon)-creates a JButton with Icon.
  • JButton(String s,Icon icon)-creates a JButton with both icon and Strings.

Java JButton Methods

  • void setText(String s)-sets JButton text.
  • String getText()-returns JButton text.
  • void setEnabled(boolean )-enables of disables the JButton.
  • void addActionListener()-adds actionlistener to the button.

Creating  Java JButton Example



How to create Java JButton swing component

Adding  ActionListener to Java JButton


 Output:Adding  ActionListener to Java JButton


About the Author: Eric K

Kenyan Developer and Blogger with Interests in Java,Javascript,Node.js, Wordpress, Android, PHP and Making Money Online. Solving world problems through programming is my passion. Hire me: Email :