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

  • SEARCH
  • Home
  • 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 6530681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:07+00:00 2026-05-25T09:47:07+00:00

I am very new to Java (only been using it for about a week)

  • 0

I am very new to Java (only been using it for about a week) and I’m working on a store simulator. Basically I just want to get basic things done right now like having a time until closing and a time since opening variable displayed on the screen.

I have made the following functions in my Simulation extends javax.swing.JFrame class :

public void incOpenTime() {
    timeOpen++;
    int hours = timeOpen / 3600;
    int minutes = (timeOpen % 3600) / 60;
    int seconds = (timeOpen % 3600) % 60;
    this.openTime.setText((hours < 10 ? "0" : "") + hours
            + ":" + (minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds < 10 ? "0" : "") + seconds);
    decTimeLeft();
    }

public void decTimeLeft() {
    int remainingTime = 28800 - timeOpen;
    int hours = remainingTime / 3600;
    int minutes = (remainingTime % 3600) / 60;
    int seconds = (remainingTime % 3600) % 60;
    this.timeLeft.setText((hours < 10 ? "0" : "") + hours
            + ":" + (minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds < 10 ? "0" : "") + seconds);

    }

The openTime and timeLeft variables using the setText() method are part of the GUI itself.

In main, I call incOpenTime like so:

while(this.timeOpen < 28800) 
{
    incOpenTime();

}

First off, when I run this it basically just goes through the loop and outputs only the last time to the screen. How would I make it so the times are constantly changing?

Secondly, I would like to delay it slightly…maybe about 1ms per second so the simulation runs slower and other data being output to the screen (later) is more readable. I’ve tried Thread.sleep(1); in the loop but it doesn’t display the numbers as they are changed.

Please help.

Thanks

EDIT:

Here’s what I did to get it working. In main:

timer = new Timer();
for(int i=1; i<= 28800; i++)
{
        timer.schedule(new task(), 1*i);

}

Made a new class:

class task extends TimerTask {

    @Override
    public void run() {
        incOpenTime();
        }
}
  • 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-05-25T09:47:08+00:00Added an answer on May 25, 2026 at 9:47 am

    You need a java.util.Timer

    Edit: As this is the accepted answer, it is important to note that the TimerTask should execute its Runnable on the event dispatch thread. The correct variation of this approach is noted in @mKorbel’s answer. There’s a short example below and a more elaborate one here. A somewhat simpler approach, suggested by @Hovercraft, is seen here.

    class Task extends TimerTask {
    
        @Override
        public void run() {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    incOpenTime();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Java, I want to add a PNG image to
I'm very new to Java but I've been developing a habit to use final
I am very new to processing.org and Java. I am trying to store objects
I'm very new to android, in fact only started yesterday. I managed to get
I am very new to programming and I am using Java as my first
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm soon going to check in the very first commit of a new Java
I am really new to Java and I read that synchronized is very expensive
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
I've been trying to write some very fast Java code that has to do

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.