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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:10:42+00:00 2026-06-17T06:10:42+00:00

I have a panel with a JTabbedpane and in every tab you can set

  • 0

I have a panel with a JTabbedpane and in every tab you can set parameters to execute a query. When one query is busy retrieving his data from the database, you can already open a new tab to set the new parameters. To avoid overload on the database only one query may be executed at once. But when you click execute the program must remember which queries to execute in the right order. During the execution a loader icon is shown and the GUI may not be frozen, because there is a stop button you can click to stop the execution.

I used a swingworker to avoid the GUI from blocking while executing the query and that works fine. But now I want to prevent the next query to start before the previous has finished. In a model, common for the whole panel, I initialized a semaphore: private final Semaphore semaphore = new Semaphore(1, true);

This is the code which starts the swingworker (I’ve added println commands to see which is started, stopped or finished)

private void doStoredQuery() {
        try {
            semaphore.acquire();
            System.out.println(queryName + "started");
            worker.execute();
        } catch (InterruptedException e) {
           throw new RuntimeException(e);
        }
    }

And this is my swingworker (initializeWorker() is called from the constructor of the main class):

private SwingWorker<StoredQueryDataModel, Integer> initializeWorker() {
    worker = new SwingWorker<StoredQueryDataModel, Integer>() {
        @Override
        protected StoredQueryDataModel doInBackground() throws Exception {
            try {
                StoredQueryDataModel dataModel = null;
                publish(0);
                try {
                    dataModel = new StoredQueryDataModel(queryRunner, ldbName, queryName, params);
                } catch (S9SQLException e) {
                    // 
                } catch (Throwable e) {
                    showErrorMessage(e);
                }
                return dataModel;
            }
            finally {
                semaphore.release();
                System.out.println(queryName + "finished");
            }
        }

        @Override
        protected void process(List<Integer> chunks) {
            //ignore chunks, just reload loader icon
            panel.repaint();
        }

        @Override
        protected void done() {
            String error;
            try {
                result = get();
                error = null;
            } catch (Exception e) {
                error = e.getMessage();
            }

            if(result == null) {
                semaphore.release();
                System.out.println(queryName + " stopped");
            }

            if(error == null) {
                // process result
            }
            else {
                showErrorMessage(new Throwable(error));
            }
        }

    };
    return worker;
}

I’ve tried putting the acquire and release on other positions in the code, but nothing seems to work. I am bot in Swingworker and sempahores quite new… Can someone help?

  • 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-17T06:10:43+00:00Added an answer on June 17, 2026 at 6:10 am

    I have found the problem: the semaphore had to be a static variable. In my code there were as many semaphores as there are tabs, which caused them to run at the same time instead of sequentially.

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

Sidebar

Related Questions

I use a jtabbedpane. for each tab it will have a panel and some
I have one tabstrip control having two tabs. On one tab i have Panel
I have a panel. in that panel there is one text box for some
I have a JTabbedPane with a custom tab component. That component contains a JLabel
I have panel with two toolbars . How can I to implement custom class
I have panel which is using flow layout. How can I make break in
I have 3 panels. One is the main panel which holds 2 smaller panels.
I have a panel containing three plots. How can I use par to specify
I have created 3 panels. In the root panel I have added JTabbedPane ,
I have an application with several components. One of them is a JTabbedPane with

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.