Ryan Turnbull Ryan Turnbull - 6 months ago 15
Java Question

Any way to delay actions in a Java GUI

i'm making a poker game for a uni assignment and i was wondering if there is any way to have a method that does as follows ( Note: very roughly written up code )

JTextArea textArea = new JTextArea();

public void printer(String s){
//I want to delay here, for 2 seconds each time i print to the jtextarea
textArea.append(s);
}

public void runGame(){
printer("Dealing cards...");
//I want to delay to add to an effect of the time it takes to actually deal the cards
pokerHand.setVisibility(true);
printer("What you like to do?");

//
//Code here containing running the game
//

printer("Daniel Negreanu folds");
//I want to have a delay here for the time it takes to make a decision.
printer("Phil Hellmuth folds");


Theres many many more instances i want to use this throughout my program, and just wondered if there is any way to do this.

Thanks in advance

EDIT: Not looking to use Thread.sleep() as it doesn't work well with gui.
EDIT2: I want the pokerHand.setVisibility(true), and other methods in my code to execute AFTER the delay, ( using a timer doesn't do this ).

Answer

Not looking to use Thread.sleep() as it doesn't work well with gui.

Good, use a Swing Timer instead

I want the pokerHand.setVisibility(true), and other methods in my code to execute AFTER the delay, ( using a timer doesn't do this ).

Yes it does, you're just not using it properly, but since you've not provided any actual code, I can't say "how" you're not using it properly, only from the sounds of it, you are.

Start by taking a look at How to use Swing Timers for more details

The following is very simple example, it uses a Timer to calculate and print the amount of time between the time you clicked the button.

The example updates the UI AFTER the Timer is started, but until the Timer completes, the calculation and result are not generated

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea ta;       
        private JButton btn;

        private Timer timer;

        private LocalTime startTime, endTime;

        public TestPane() {
            setLayout(new BorderLayout());
            ta = new JTextArea(10, 20);
            add(new JScrollPane(ta));
            btn = new JButton("Click");
            add(btn, BorderLayout.SOUTH);

            timer = new Timer(2000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    endTime = LocalTime.now();
                    Duration duration = Duration.between(startTime, endTime);
                    ta.append("Ended @ " + endTime + "\n");
                    ta.append("Took " + (duration.toMillis() / 1000) + " seconds\n");
                }
            });
            timer.setRepeats(false);

            ta.setEditable(false);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.start();
                    startTime = LocalTime.now();
                    btn.setEnabled(false);
                    ta.append("Start @ " + startTime + "\n");
                }
            });
        }

    }

}
Comments