Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Generating Actionevent in Java

Tags:

java

awt

I'm making an App. in java , in which there is a Button to which I have added an actionlistener. The ActionEvent it(the button) generates executes some code. Now I want this piece of code to run whenever the app. starts and without pressing the button. I mean, I want to generate the Actionevent (without pressing the button) so that the piece of code the ActionPerformed contains gets executed as the app. start. After that, it may run whenever I press the button.

like image 908
pankaj Avatar asked Aug 07 '26 00:08

pankaj


2 Answers

You can create ActionEvents like any other Java Object by just using the constructor. And then you can send them directly to the component with Component.processEvent(..)

However, in this case I think you are better making your code into a separate function which is called both:

  • By the ActionListener when the button is pressed
  • Directly by your application startup code (possibility using SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait() if you need it to happen on the event-handling thread)

This way you don't mix up presentation logic with the business logic of whatever the code does....

like image 158
mikera Avatar answered Aug 10 '26 00:08

mikera


Yes it can be done, but it doesn't really make sense, since your goal isn't to press a button or to call an ActionListener's code, but rather to have a common behavior on button press and on program start up. To me the best way to achieve this is to have a method that is called by both the actionPerformed method of the ActionListener and by the class at start up.

Here's a simple example. In the code below, a method disables a button, turns the JPanel green, and starts a Timer that in 2 seconds enables the button and resets the JPanel's background color to its default. The method that causes this behavior is called both in the main class's constructor and in the JButton's ActionListener's actionPerformed method:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionOnStartUp extends JPanel {
   private static final int PANEL_W = 400;
   private static final int PANEL_H = 300;
   private static final int TIMER_DELAY = 2000;
   private JButton turnGreenBtn = new JButton("Turn Panel Green for 2 seconds");;

   public ActionOnStartUp() {
      turnPanelGreen();

      turnGreenBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            turnPanelGreen();
         }
      });
      add(turnGreenBtn);
   }

   public void turnPanelGreen() {
      setBackground(Color.green);
      turnGreenBtn.setEnabled(false);
      new Timer(TIMER_DELAY, new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            setBackground(null);
            turnGreenBtn.setEnabled(true);
            ((Timer) ae.getSource()).stop(); // stop the Timer
         }
      }).start();
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PANEL_W, PANEL_H);
   }

   public static void createAndShowGui() {
      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ActionOnStartUp());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
like image 44
Hovercraft Full Of Eels Avatar answered Aug 09 '26 22:08

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!