Jbutton in java swing actionlistener

Содержание
  1. Java Button Click Event Tutorial For Beginners-JButton ActionListener
  2. Java Button Click Event
  3. What is An Event ??
  4. Event Source
  5. Event Listener
  6. Event Classes
  7. Event Classes and Associated Listener Interfaces
  8. Handling Java Swing Button Click Event Step by Step
  9. Importing Packages
  10. Creating a class MainClass.java
  11. Creating another class ActionEventDemo.java
  12. Adding Java Button to JFrame
  13. Implementing ActionListener Interface
  14. Registering ActionListener to the JButton
  15. Performing Action Event
  16. Frequently Asked Questions
  17. Class JButton
  18. Nested Class Summary
  19. Nested classes/interfaces declared in class javax.swing.AbstractButton
  20. Nested classes/interfaces declared in class javax.swing.JComponent
  21. Nested classes/interfaces declared in class java.awt.Container
  22. Nested classes/interfaces declared in class java.awt.Component
  23. Field Summary
  24. Fields declared in class javax.swing.AbstractButton
  25. Fields declared in class javax.swing.JComponent
  26. Fields declared in class java.awt.Component
  27. Fields declared in interface java.awt.image.ImageObserver
  28. Fields declared in interface javax.swing.SwingConstants
  29. Constructor Summary
  30. Method Summary
  31. Methods declared in class javax.swing.AbstractButton
  32. Methods declared in class javax.swing.JComponent
  33. Methods declared in class java.awt.Container
  34. Methods declared in class java.awt.Component
  35. Methods declared in class java.lang.Object
  36. Constructor Details
  37. JButton
  38. JButton
  39. JButton
  40. JButton
  41. JButton
  42. Method Details
  43. updateUI
  44. getUIClassID
  45. isDefaultButton
  46. isDefaultCapable
  47. setDefaultCapable
  48. removeNotify
  49. paramString
  50. getAccessibleContext

Java Button Click Event Tutorial For Beginners-JButton ActionListener

Hello Friends, In this tutorial, you will learn about Java Button Click Event in Java Swing. In my previous tutorial, you have seen how to create the Java Button in Swing, and if you haven’t seen my previous tutorial yet, then you can click on the link given below.

So you have learned how to create a Button in Java, But You don’t know How to perform JButton on click action on button in Java Swing which means if you click on the button, nothing happens because you have not added any action event associated with the JButton. So now in this tutorial, you are going to learn about Java Button Click Event or we can say Java button pressed Event using the JFrame step by step.

So Let’s start our Tutorial Java Button Click Event in Java Swing or JButton Click Event using JFrame.

Watch Video Tutorial

  • 1 Java Button Click Event
    • 1.1 What is An Event ??
    • 1.2 Event Source
    • 1.3 Event Listener
    • 1.4 Event Classes
    • 1.5 Event Classes and Associated Listener Interfaces
    • 2.1 Importing Packages
    • 2.2 Creating a class MainClass.java
    • 2.3 Creating another class ActionEventDemo.java
    • 2.4 Adding Java Button to JFrame
    • 2.5 Implementing ActionListener Interface
    • 2.6 Registering ActionListener to the JButton
    • 2.7 Performing Action Event

    Java Button Click Event

    What is An Event ??

    • It is an object which describes a change in a source.
    • Basically they are associated with a component.
    • Some examples of the events are Java Button Pressed Event, entering a character via keyboard, selecting an item in a list , clicking the mouse etc.
    • Events are supported by number of packages including java.util, java.awt, java.awt.event .

    Event Source

    • A source is an object that generates an Event .
    • This happens when the internal state of the object changes in some way.
    • A source can generate one or more types of events.
    • A method getSource() is associated with all events which returns the object on which the event is initially occurred.

    Event Listener

    Event Classes

    • Classes that represent events.
    • EventObject is the superclass for all events which means it is the root of the Java event class hierarchy.
    • EventObject contains two methods. getSource() and toString().
    • getSource() method returns the source and toString() method returns the string equivalent of the event .
    • AWTEvent is a subclass of EventObject class and it is superclass for all AWT events .
    • Package java.awt.event is used for defining interfaces and classed that is used for event handling in AWT and SWING.
    • Some common event classes in the package java.awt.event are given below :

    Event Classes and Associated Listener Interfaces

    So this was the brief description of event classes and listeners, and now we will see Java Button Click Event or Swing JButton click action using JFrame step by step in which we will learn about the JButton ActionListener interface, ActionPerformed() method and addActionListener() method.

    Handling Java Swing Button Click Event Step by Step

    Importing Packages

    • In the first step, we need to import all essential packages. In this program, we need to import another new package java.awt.event because we are dealing with event handling and this package provides classes and interfaces that are used for event handling in awt and Swing.

    Creating a class MainClass.java

    • In this step, create a class (MainClass.java in this example), and inside that class, there will be our main method.

    Creating another class ActionEventDemo.java

    • In this step, create another separate class (ActionEventDemo.java in this example).
    • Now create an object of the JFrame class.
    • Create a user-defined method prepareGUI(), and inside that method we will set the properties of the JFrame class like its title, its location and size, its default close operation, its visibility etc.
    • We will also change the Layout Manager of the frame’s content pane to null. By default frame’s content pane uses BorderLayout Manager as its Layout Manager.
    • Now we will create the constructor of the class ActionEventDemo and inside that constructor call the prepareGUI() method.

    Java Button Click Event

    • Now save your program, compile it and run it.
    • As you can see that we have successfully created our JFrame.
    • In next step, we will add a Java button to our JFrame.

    Adding Java Button to JFrame

    • Create an object of the JButton class.
    • Now again create another user-defined method buttonProperties() and inside that method set the location and size of the JButton using setBounds() method and finally add the JButton to the JFrame using add() method.

    Java Button Click Event

    • Now save your program, compile it and run it.
    • As you can see that we have successfully created and added the JButton to the JFrame.
    • Now we want that when we click on the button, some activity should be performed which means how to make a JButton do something when JButton is clicked.
    • For this, first of all, we need to implement the JButton Listener which means ActionListener interface into our class so that we will be able to do some JButton click event..

    Implementing ActionListener Interface

    • If we have implemented the ActionListener interface in any class, then we must have to override its method which is actionPerformed(ActionEvent e) which takes a parameter ActionEvent (a class defined in package java.awt.event ). Now when someone clicks on the button the actionPerformed() method is called.
    • Now let’s implement the ActionListener interface into our class ActionEventDemo.

    Registering ActionListener to the JButton

    • In this step, we will add or can say register ActionListener to the JButton.
    • For this, we have to call addActionListner() method using the object of the JButton class. The parameter of the addActionListener() method is the object of that class in which ActionListener interface is implemented or can say in which we have defined actionPerformed() method. So if we are in the same class in which ActionListener interface is implemented, then we will pass this as an argument.

    Performing Action Event

    • Now we want that if we click on the button the background color of the frame’s content pane should be changed. For this, we will write the desired codes inside the actionPerformed() method. Which means the behaviour we want in response to the action is coded inside the actionPerformed() method.

    Java Button Click Event

    • Now save the program, compile it and run it.
    • Now we can see that as soon as we click on the button, the background color of the JFrame has been changed to pink.

    Frequently Asked Questions

    The Answer is “Action Event”. The associated Listener interface is the ActionListener interface. When the user clicks on the button then the button fires an Action Event and that results in invocation of the ActionListener’s acitonPerformed() method.

    Click here for the step by step tutorial on How to Open a new JFrame on Button Click in Java.

    The Answer is “actionPerformed()” method. This method is invoked automatically when the button is clicked, and you can define the specific actions you want to perform inside this method.

    The Answer is “addActionListener()” method. addActionListener() method registers the ActionListener to the Java Button and the behaviour we want in response to the action is coded inside the “actionPerformed() method. The actionPerformed() method is called just after the user executes any action.

    So, guys, I am wrapping up this tutorial “Java Button Click Event” in Java Swing and Feel free to drop us a comment if you find out something difficult to understand. After learning this, you can also learn how to create a Login Form in Java Swing and GUI Number Guessing Game in Java.

    People are also Reading…..

    • Best Java Books For Beginners
    • Menu Driven Program in Java Using Switch Case
    • How to Create Calculator in Java Swing
    • How to Create Tic Tac Toe Game in Java
    • How to Create Login Form in Java Swing
    • Registration Form In Java with Database Connectivity
    • How to Create Splash Screen In Java
    • How to Create Mp3 Player in Java
    • How to Connect MySQL Database in Java Using NetBeans
    • 11 Best Site to Learn Java Online for Free

    Источник

    Class JButton

    Buttons can be configured, and to some degree controlled, by Action s. Using an Action with a button has many benefits beyond directly configuring a button. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

    See How to Use Buttons, Check Boxes, and Radio Buttons in The Java Tutorial for information and examples of using buttons.

    Warning: Swing is not thread safe. For more information see Swing’s Threading Policy.

    Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder .

    Nested Class Summary

    Nested classes/interfaces declared in class javax.swing.AbstractButton

    Nested classes/interfaces declared in class javax.swing.JComponent

    Nested classes/interfaces declared in class java.awt.Container

    Nested classes/interfaces declared in class java.awt.Component

    Field Summary

    Fields declared in class javax.swing.AbstractButton

    Fields declared in class javax.swing.JComponent

    Fields declared in class java.awt.Component

    Fields declared in interface java.awt.image.ImageObserver

    Fields declared in interface javax.swing.SwingConstants

    Constructor Summary

    Method Summary

    Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane .

    Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane , and if so, sets the RootPane ‘s default button to null to ensure the RootPane doesn’t hold onto an invalid button reference.

    Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane.

    Methods declared in class javax.swing.AbstractButton

    Methods declared in class javax.swing.JComponent

    Methods declared in class java.awt.Container

    Methods declared in class java.awt.Component

    Methods declared in class java.lang.Object

    Constructor Details

    JButton

    JButton

    JButton

    JButton

    JButton

    Method Details

    updateUI

    getUIClassID

    @BeanProperty(bound=false, expert=true, description=»A string that specifies the name of the L&F class.») public String getUIClassID ()

    isDefaultButton

    @BeanProperty(bound=false, description=»Whether or not this button is the default button») public boolean isDefaultButton ()

    Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane . Most look and feels render the default button differently, and may potentially provide bindings to access the default button.

    isDefaultCapable

    setDefaultCapable

    @BeanProperty(visualUpdate=true, description=»Whether or not this button can be the default button») public void setDefaultCapable (boolean defaultCapable)

    Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane. The default value of the defaultCapable property is true unless otherwise specified by the look and feel.

    removeNotify

    Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane , and if so, sets the RootPane ‘s default button to null to ensure the RootPane doesn’t hold onto an invalid button reference.

    paramString

    Returns a string representation of this JButton . This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null .

    getAccessibleContext

    @BeanProperty(bound=false, expert=true, description=»The AccessibleContext associated with this Button.») public AccessibleContext getAccessibleContext ()

    Gets the AccessibleContext associated with this JButton . For JButton s, the AccessibleContext takes the form of an AccessibleJButton . A new AccessibleJButton instance is created if necessary.

    Report a bug or suggest an enhancement
    For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
    Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
    Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
    All rights reserved. Use is subject to license terms and the documentation redistribution policy.

    Источник

    Читайте также:  Php add variable to variable
Оцените статью