DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2006

    Invoking Action Listeners via code

    Hi guys,

    I have a question about action listeners and i've been searching forever but can't seem to find anything to help me. Basically, I have a Jbutton setup, but upon certain instances, when a boolean flag is set, i need to automatically invoke the button via code.

    Example: I am creating a simple board game that requires AI. But the interface is all Jbuttons. So when it is the computer's turn, i need to be able to invoke the Jbutton (basically, I need to pretend the computer had a mouse and pushed the button itself), but i'm not sure how to go about doing that. Any help would be greatly appreciated .

    I grabbed some sample code off java's website, perhaps someone could show me how it would work in this example?

    public class SwingApplication implements ActionListener {
    JButton button = new JButton("I'm a Swing button!");

    public void actionPerformed(ActionEvent e) {
    label.setText(labelPrefix + numClicks);


  2. #2
    Join Date
    Feb 2006
    Cologne - Germany
    on jdk documentation for jbutton you will recognize, that a jbutton inherits from a class that has the method fireActionPerformed. This one you can invoke.
    but it's a better aproach to make the action in an seperate method:
      public myclass(){
        mybutton = new JButton();
          new actionlsitener(){
            public actionperformed(){
      public void doaction(){
        // do sthg when button is pressed
    so the code for the action really performed (here doaction()) can be accessed anywhere and you don't have to bother about pressing buttons.

Similar Threads

  1. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 12:00 AM
  2. Speaking of strings...
    By Harlow in forum .NET
    Replies: 246
    Last Post: 10-26-2002, 12:30 AM
  3. Will VB.NET be more stable than VB6?
    By Jason in forum .NET
    Replies: 125
    Last Post: 10-05-2002, 04:34 PM
  4. TNX Rob - Re: Security of non-server code?
    By Patrick Ireland in forum .NET
    Replies: 19
    Last Post: 04-24-2001, 04:19 PM
  5. Invoking a system menu item through code
    By Dharmesh Sanghavi in forum VB Classic
    Replies: 0
    Last Post: 02-28-2001, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.