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 9099371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:35:55+00:00 2026-06-17T00:35:55+00:00

I have an idea to create widgets that perform tasks and can be added

  • 0

I have an idea to create widgets that perform tasks and can be added to the view but I’m worried I could be approaching the problem from completly the wrong angle. Here is my abstract class

public abstract class AbstractWidget extends AnchorPane
{
    private Thread thread;
    protected Task<?> task;

    public AbstractWidget()
    {
    }

    public void start()
    {
        this.thread = new Thread(this.task);
        this.thread.start();
    }

    public void stop()
    {
        this.task.cancel();
    }
}

an implementation (widget that tracks how long the program has been running)

public class RuntimeWidget extends AbstractWidget
{
    public RuntimeWidget()
    {
        this.task = new Task<Void>()
        {
            @Override
            public void run()
            {
                final long startTime = System.currentTimeMillis();

                while(true)
                {
                    if (isCancelled())
                        break;

                    Platform.runLater(new Runnable()
                    {
                        @Override
                        public void run ()
                        {
                            long secs = System.currentTimeMillis() -startTime) / 1000;
                            String display = String.format("%02d:%02d", (secs % 3600) / 60, (secs % 60));
                            System.out.println(display);
                        }
                    });

                    try {
                        Thread.sleep(1000);
                    } catch ( InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }

}

Using the widget in the FXML controller

public void initialize(URL fxmlFileLocation, ResourceBundle resources)
{
    RuntimeWidget runtimeWidget = new RuntimeWidget();
    gridPane.add(runtimeWidget, 0, 0);
}

@FXML private void handleRunAction( ActionEvent event ) throws IOException, InterruptedException
{
    runtimeWidget.start();
}

Everything works completely fine but is this the right way? I used Task instead of Service because the program’s run action can be stopped and started all over again, but never paused and resumed.

  • 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-17T00:35:56+00:00Added an answer on June 17, 2026 at 12:35 am

    I decided to just use Service. It’s much easier to handle functions that can be run multiple times.

    public abstract class AbstractWidget extends Whatever
    {
        protected Service<?> service;
    
        public AbstractWidget()
        {
        }
    
        public void start()
        {
            service.start();
        }
    
        public void stop()
        {
            service.cancel();
            service.reset();
        }
    }
    

    Then you can create a class like:

    public class MyWidget() extends AbstractWidget
    {
    
        public MyWidget()
        {
            this.service. = new MyService();
        }
    
        private class MyService extends Service<Void>
        {
            protected Task<Void> createTask()
            {
                @Override
                return new Task<Void>()
                {
                    // whatever
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have any idea how can I create a custom UIPickerView with an
Does anyone have any idea on how can you create a product filtering query
I'm trying to create a depth of field post process, but have no idea
In short, when you have planned or have thought about an idea to create
I'm building a Qt application that have about 30 different views (QWidgets). My idea
I have some JavaScript code that is supposed to run on document.ready but I
I have the following problem, maybe someone can help me (or explain, where my
I found this nifty plugin that lets me create dashboard widgets from external files.
We are currently facing the following problem: We have an application that needs to
I have an idea to create a Parental Supervision Web Browser. The way it

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.