Home > Blockchain >  JLabel text not being displayed correctly after some quickly repeated action event calls
JLabel text not being displayed correctly after some quickly repeated action event calls

Time:12-12

I'm coding a GUI for my Tic Tac Toe project and I'm having fun toying with little visual effects like the one below. Every time the user sets a cpu player and clicks on a done button forgetting to set cpu strength level, I display a little warning message in the same way my code sample below does.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TestingStuff implements ActionListener {

JLabel myLabel;
JFrame myFrame;
JButton myButton;
JPanel myPanel;

public TestingStuff() {
    myFrame = new JFrame("Hello");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    myPanel.setBackground(Color.DARK_GRAY);
    myFrame.add(myPanel, BorderLayout.CENTER);

    myLabel = new JLabel("Hi dear stackoverflow coders!");
    myLabel.setFont(new Font("MV Boli", Font.BOLD, 15));
    myLabel.setForeground(Color.GREEN);
    myPanel.add(myLabel);

    myButton = new JButton("Click me like there's no tomorrow!");
    myButton.addActionListener(this);
    myFrame.add(myButton, BorderLayout.SOUTH);

    myFrame.pack();
    myFrame.setLocationRelativeTo(null);
    myFrame.setVisible(true);
}

    @Override
public void actionPerformed(ActionEvent e) {
    int timerDelay = 1;
    Timer blinkTimer = new Timer(timerDelay, new ActionListener() {
        private int count = 0;
        private int maxTime = 30;
        private String hello = myLabel.getText();
        private int len = hello.length();
        private String s;

        public void actionPerformed(ActionEvent e) {
            if (count * timerDelay >= maxTime) {
                ((Timer) e.getSource()).stop();
            } else {
                myLabel.setVisible(true);
                if (count < len) {
                    s = hello.substring(0, count   1);
                    myLabel.setText(s);
                }
                count  ;
            }
        }
    });
    blinkTimer.start();
}

    public static void main(String[] args) {
    TestingStuff foo = new TestingStuff();
    }
}

Try to repeatedly click on the button.

Before clicking: enter image description here

After clicking: enter image description here

At least on my machine. Since there's no reason for the user to repeatedly click on the done button, I'm not too worried, but still... I think I'll end up disabling every button the user can click during that short animation in order to avoid unexpected behaviour. My question: can anybody explain what is happening and why I get to see that truncated text just after let's say 7-8 fast clicks?

CodePudding user response:

The delay between updates is set to 1 millisecond (got luck getting that to be that accurate

  • Related