E. V. d. B.
E. V. d. B.

Reputation: 815

Disabling JComponents on a JPanel in for-loop: annoying delay

I have a JFrame with a few JPanels. One is the boardPanel with a GridLayout of JButtons (4 by 4). The other is the controlPanel which has a FlowLayout which containt a JTextField and two JButtons.

I have two methods enableBoard() and disableBoard(). In those methods I setEnabled() each component in the boardPanel and controlPanel to true or false, via a for-loop. This all happens while a swing Timer is running. When I start the timer and thus start the enableBoard() (it was disabled via disableBoard) it kinda works, but often has a delay, so not all buttons get disabled at the same moment, I suspect it has something to do with the Timer because the delay seems to be happening in seconds... Here are the important methods:

public void disableBord() {
    Component[] boardcomps = boardPanel.getComponents();
    for(int i = 0; i < bordcomps.length; i++) {
        boardcomps[i].setEnabled(false);
    }

    Component[] checkComps = controlPanel.getComponents();
    for(int i = 0; i < checkComps.length; i++) {
        checkComps[i].setEnabled(false);
    }
}

public void enableBord() {
    Component[] boardcomps = boardPanel.getComponents();
    for(int i = 0; i < bordcomps.length; i++) {
        boardcomps[i].setEnabled(true);
    }

    Component[] checkComps = controlPanel.getComponents();
    for(int i = 0; i < checkComps.length; i++) {
        checkComps[i].setEnabled(true);
    }
}

And the Timer that runs:

timer = new Timer(1000, new ActionListener() {
        int time = game.getSeconds()+4;
        @Override
        public void actionPerformed(ActionEvent e) {
            if (time == game.getSeconds()+4) {
                lblFeedback.setText("3");
                lblTime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+3) {
                lblFeedback.setText("2");
                lbltime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+2) {
                lblFeedback.setText("1");
                lblTime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+1) {
                lblFeedback.setText("Start!");
                lblTime.setText(game.secToMinSec(time));
                time--;
                enableBord();
            } else if(time == 0) {
                lblTime.setText("0:00");
                lblFeedback.setText("Game finished!");
                disableBord();
                game.endeGame();
                timer.stop();
            } else if (time ==  game.getSeconds()) {
                lblTime.setText(game.secToMinSec(time));
                time--;
            } else {
                lblTime.setText(game.secToMinSec(time));
                time--;
            }
        }
    });

The delay looks like this:

Delay when disabling JComponents

Upvotes: 0

Views: 216

Answers (1)

E. V. d. B.
E. V. d. B.

Reputation: 815

A proper repaint(); after every setEnable() in the loop seemed to have solved my problem!

Upvotes: 1

Related Questions