Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a game from within an action listener

I have a Blackjack game that I've made in Java and I want to signal the start of the game by clicking a button. All my action listeners work just fine and all that, but the problem lies in that I can't figure out how to start the game without it running completely within the actionPerformed method. Obviously, a function continuously running within the actionPerformed method will effectively disable the rest of my GUI. Here's a code snippet....

go.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
                 // START GAME SOMEHOW but must run outside of action listener
        }
    });
like image 879
Keith Salmon Avatar asked May 11 '12 20:05

Keith Salmon


People also ask

How do I listen to events generated by ActionListener?

Register the listener to listen for events generated by the event source (e.g. by responding to the user clicking the button through the ActionListener). 4. Define methods for handling events (e.g. in actionPerformed in ActionListener).

What is the actionPerformed method in ActionListener?

This results in the invocation of the action listener's actionPerformed method (the only method in the ActionListener interface). The single argument to the method is an ActionEvent object that gives information about the event and its source.

How to create event listeners in Java Swing programming?

In Java Swing programming, by registering listeners, we can listen for events generated by event sources, thus handling user behavior that we need to deal with in event handlers. 1. Create a new component (e.g. JButton). 2. Add the component to the appropriate panel (such as JPanel). 3.

Is there an adapter class for ActionListener?

Because ActionListener has only one method, it has no corresponding adapter class. Called just after the user performs an action. Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommand that lets you set this string.


2 Answers

Obviously, a function continuously running within the actionPerformed method will effectively disable the rest of my GUI.

This is a valid observation and shows that you have understand the fundamental rule when working with Swing.

Your game is most likely event driven (correct me if I'm wrong) so the action performed by the button should just set the program in a new state, waiting for further events. This is nothing that should be time consuming, and is typically done directly by the EDT.

Of course, if you want to do a fancy start-new-game animation, that needs to be performed in a separate thread, in which case you simply start the animation thread (I would recommend using a SwingWorker though) from within the actionPerformed method, and then return.

In code, I imagine it would look something like this:

go.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        // Remove the menu components
        someJPanel.removeAll();

        // Show the game table
        someJPanel.add(new GamePanel());

        someJPanel.revalidate();
        someJPanel.repaint();

        // done. Wait for further user actions.
    }
});
like image 152
aioobe Avatar answered Sep 27 '22 23:09

aioobe


You game should probably start in its own thread and manage that itself (hard to say), but to get you going you could start your game in a new "external" thread, something like this in your actionPerformed:

public void actionPerformed(ActionEvent e) {
    Thread thread = new Thread("Game thread") {
        public void run() {
            startGame(); //or however you start your game
        }
    };
    thread.start();
}
like image 44
Mattias Isegran Bergander Avatar answered Sep 27 '22 21:09

Mattias Isegran Bergander