CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JButton in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JButton in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X
git change remote url

The JButton class is used to create a labeled button that has a platform-independent implementation. The application results in some action when the button is pushed. It inherits AbstractButton class.

Constructors

ConstructorDescription
JButton()Creates a button with no set text or icon.
JButton(Action a)Creates a button where properties are taken from the Action supplied.
JButton(Icon i)Creates a button with an icon.
JButton(String s)Creates a button with text.
JButton(String s, Icon i)Creates a button with initial text and an icon.

Common inherited methods from AbstractButton

MethodDescription
void setText(String s)Sets the text for the button.
String getText()Returns the button text.
void setEnabled(boolean b)It determines whether to enable or disable the button.
void setIcon(Icon i)Sets the button’s default icon.
Icon getIcon()Returns the default icon.
void setMnemonic(int i)Sets the keyboard mnemonic on the current model. The mnemonic is the key which when combined with the look and feel’s mouseless modifier (usually Alt) will activate this button if focus is contained somewhere within this button’s ancestor window.
void addActionListener(ActionListener a)Adds a ActionListener to the button.

Example

public class JButtonDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame("JButton Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setLayout(null);
		
		JButton button = new JButton();
		button.setText("Click me");
		button.setBounds(100, 100, 100, 20);
		frame.add(button);
		
	}
}

Output

Swing JButton

Related Posts:

  • java.lang.Boolean class in Java
  • Inheritance in Java: A Developer’s Guide to Code Reusability
  • Constructor in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Boolean Data Type in Java
  • Java Swing Tutorial
Tags:guijavaswing
Was this article helpful?
← Previous ArticleDifference between AWT and Swing
Next Article →Short Circuit Logical Operator in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap