Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:36:08+00:00 2026-06-09T05:36:08+00:00

Okay I have tried everything now. I still can’t get my timer to update!…Here

  • 0

Okay I have tried everything now. I still can’t get my timer to update!…Here is what I have so far..
Things I have tried so far:
A separate thread (Which worked but I really MUST use the java.swing.Timer)
Timer as seen below which is not working
java.util.timer.
Any suggestions would be helpful
import sudoku.Sudoku_Board;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Timer;

public class Sudoku_GUI extends JFrame {

    class NumberedField extends JTextField {
        public int row = -1, col = -1;
        public void setRowAndCol(int _row, int _col) {
            row = _row;
            col = _col;
        }
    }

    private static final long serialVersionUID = 1L;
    boolean paused = false;
    Timer timer = null;
    int minutes_elapsed = 0, seconds_elapsed = 0;
    NumberedField field[][];
    Sudoku_Board Puzzle_Board, Solution_Board;
    JLabel time_label, time;
    JButton pause;
    public Sudoku_GUI() {

        super("fokleSudoku");
        setSize(420, 480);
        setResizable(false);

        Solution_Board = new Sudoku_Board();
        Puzzle_Board = new Sudoku_Board();

        time_label = new JLabel("Time: ");
        time_label.setBounds(new Rectangle(20, 8, 100, 20));
        add(time_label);
        time_label.setVisible(false);

        time = new JLabel();
        time.setBounds(new Rectangle(60, 8, 100, 20));
        time.setVisible(false);
        time.setForeground(Color.BLUE);
        add(time);

        JButton new_game = new JButton("New Game");
        new_game.setBounds(new Rectangle(20, 400, 100, 30));
        new_game.setBackground(Color.BLACK);
        new_game.setForeground(Color.WHITE);
        add(new_game);

        new_game.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Solution_Board.generate();
                Puzzle_Board.random_remove(Solution_Board);
                apply_fields(Puzzle_Board);
                time_label.setVisible(true);
                pause.setVisible(true);
                if(paused)
                    unpause();
                start_timer(0, 0);

            }

        });

        pause = new JButton("Pause");
        pause.setBounds(new Rectangle(280, 400, 100, 30));
        pause.setBackground(Color.red);
        pause.setForeground(Color.WHITE);
        pause.setVisible(false);
        pause.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                if(paused){
                    unpause();
                    start_timer(minutes_elapsed, seconds_elapsed);
                }
                else {
                    pause();
                }

            }
        });
        add(pause);

        setLayout(null);

        int x = 20, y = 35;
        field = new NumberedField[9][9];
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                field[row][col] = new NumberedField();
                field[row][col].setBounds(x, y, 40, 40);
                field[row][col].setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
                field[row][col].setRowAndCol(row, col);
                field[row][col].setHorizontalAlignment(JTextField.CENTER);
                field[row][col].setInputVerifier(new InputVerifier() {
                    public boolean verify(JComponent arg0) {
                        NumberedField curr_field = (NumberedField) arg0;
                        if (curr_field.getText().length() != 1)
                            curr_field.setText("");
                        else if (!(curr_field.getText().charAt(0) >= '0' && curr_field
                                .getText().charAt(0) <= '9'))
                            curr_field.setText("");
                        else if (curr_field.getText().charAt(0) >= '0'
                                && curr_field.getText().charAt(0) <= '9') {
                            final int temp[] = Puzzle_Board.validValue(
                                    Integer.parseInt(curr_field.getText()),
                                    curr_field.row, curr_field.col);
                            if (temp[0] != 808 && temp[1] != -1) {
                                new Thread(new Runnable() {
                                    public void run() {
                                        Color original_color = field[temp[0]][temp[1]]
                                                .getBackground();
                                        field[temp[0]][temp[1]]
                                                .setBackground(Color.RED);
                                        try {
                                            Thread.sleep(2000);
                                        } catch (InterruptedException e) {
                                        }
                                        field[temp[0]][temp[1]]
                                                .setBackground(original_color);
                                    }

                                }).start();

                            } else {
                                Puzzle_Board.setValue(
                                        Integer.parseInt(curr_field.getText()),
                                        curr_field.row, curr_field.col);
                            }
                        }
                        return true;

                    }

                });
                add(field[row][col]);
                x += 40;

                if (col % 3 == 0 && (row) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 2, 1, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0 && (row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 2, 2, Color.BLACK));
                else if (col % 3 == 0 && (row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 2, 2, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0 && row % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 1, 1, 2, Color.BLACK));
                else if (row % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 1, 1, 1, Color.BLACK));
                else if ((row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 2, 1, Color.BLACK));
                else if (col % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 2, 1, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 1, 2, Color.BLACK));
                else
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 1, 1, Color.BLACK));
                if ((col + 1) % 9 == 0) {
                    y += 40;
                    x = 20;
                }

            }
        }

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void apply_fields(Sudoku_Board sudoku_board) {
        empty_fields();
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (sudoku_board.getValue(row, col) != -255) {
                    field[row][col].setText(String.valueOf(sudoku_board
                            .getValue(row, col)));
                    field[row][col].setEditable(false);
                }
            }
        }
    }

    public void fill_answer() {
        apply_fields(Solution_Board);
    }

    public void empty_fields() {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                field[row][col].setText("");
                field[row][col].setBackground(Color.WHITE);
                field[row][col].setEditable(true);
            }
        }
    }

    public void start_timer(int minutes, int seconds){
        if(timer == null){
             timer = new Timer(0, new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    time.setText(String.format("%02d:%02d", minutes_elapsed,seconds_elapsed));
                    seconds_elapsed++;
                    if(seconds_elapsed == 60){
                        seconds_elapsed = 0;
                        minutes_elapsed++;
                    }
                }
             });
             timer.setDelay(1000);
             timer.setRepeats(true);
        }
             timer.start();
    }

    public void stop_timer(){
        if(timer != null){
            timer.stop();
        }
    }

    public void pause(){
        if(!paused){
            paused = true;
            pause.setText("Resume");
            stop_timer();
            empty_fields();
        }
    }

    public void unpause(){   // DOES NOT START THE TIMER !! -- NEEDED IN SETTING UP A NEW GAME.
        if(paused){
            paused = false;
            pause.setText("Pause");
            apply_fields(Puzzle_Board);
        }
    }

    public static void main(String... args) throws InterruptedException {
        new Sudoku_GUI();
    }
}

Here is the sudoku board file if you want it for debugging…

package sudoku;

import java.util.ArrayList;
import java.util.Random;

public class Sudoku_Board {
    private int Board[][];

    public Sudoku_Board() {
        Board = new int[9][9];
        clear();
    }

    public void print() {

    }

    public int[] validValue(int value, int row, int col) { // (-1,-1) invalid
        int index[] = {-1,-1};
        return index;
    }

    public void random_remove(Sudoku_Board obj) {

    }

    public void clear() {

    }
    public void generate() {

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:36:09+00:00Added an answer on June 9, 2026 at 5:36 am

    You have a time.setVisible(false); but no time.setVisible(true);. It seems that you are making the label which displays Time: as visible but the label which displays the actual time is never made visible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I have read, and tried a lot of things on how to implement
Okay - I have a dilemma. So far my script converts page titles into
Okay I have updated my code a little, but I am still not exactly
Okay i have two models: posts and comments. as you can think comments has
Okay, I have been messing with this autocomplete stuff for 2 weeks now, and
I have some problems with ajax responses. Everything is working okay, however I got
Okay so I have a class that has 'weak typing' I.E. it can store
Okay I have a list of devices where i can select which to edit.
Okay, I have tried a dozen different ways and no success. I want to
Okay, so, here's my problem: I have developed a .NET 4.0 (Client Profile) +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.