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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:27:31+00:00 2026-06-04T04:27:31+00:00

ok, I’m here again to ask one dummy question. I have a custom dialog

  • 0

ok, I’m here again to ask one dummy question. I have a custom dialog frame and have progress bar on it. So also I have a genetic algorithm (which works pretty big amount of time).

As I understand all the situation, I need to execute separate threads for algorithm, progress bar and leave the main thread for the dialog (to give him an opportunity to respond to user actions). So I tried to implement this, and got the following:

public class ScheduleDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();

    private static Data data;
    private static GeneticEngine geneticEngine;
    private static Schedule schedule;

    private static JProgressBar progressBar;
    private static JLabel statusLabel;

    public static void showProgress() {
        try {
            ScheduleDialog dialog = new ScheduleDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            startScheduleComposing();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void startScheduleComposing() {
        BackGroundWorker backGroundWorker = new BackGroundWorker();
        GeneticEngineWorker geneticEngineWorker = new GeneticEngineWorker();
        new Thread(geneticEngineWorker).start();
        new Thread(backGroundWorker).start();
    }

    private ScheduleDialog() {

        this.data = Data.getInstance();

        setTitle("");
        setModal(true);
        setBounds(100, 100, 405, 150);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = getSize();
        setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new GridLayout(0, 1, 0, 0));
        {
            progressBar = new JProgressBar();
            progressBar.setBackground(Color.RED);
            contentPanel.add(progressBar);
        }
        {
            statusLabel = new JLabel("");
            contentPanel.add(statusLabel);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("Прервать");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
        }


    }

    protected static class BackGroundWorker implements Runnable {
        @Override
        public void run() {
          while(true){
            int barValue = 100 - (geneticEngine.getCurrentBestFitness() / geneticEngine.getInitialFitness());
            progressBar.setValue(barValue);
            progressBar.repaint();
            statusLabel.setText(String.valueOf(geneticEngine.getCurrentBestFitness()));
            statusLabel.repaint();
            try{
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();  
            }
          }
        }
    }

    protected static class GeneticEngineWorker implements Runnable{

        @Override
        public void run() {
            geneticEngine = new GeneticEngine(data);
            schedule = geneticEngine.GeneticAlgorithm();
        }
    }
}

it seems, that new Thread(geneticEngineWorker).start(); is working but nothing happens with progress bar. I think this part of code:

private static void startScheduleComposing() {
            BackGroundWorker backGroundWorker = new BackGroundWorker();
            GeneticEngineWorker geneticEngineWorker = new GeneticEngineWorker();
            new Thread(geneticEngineWorker).start();
            new Thread(backGroundWorker).start();
        }

we get new Thread(backGroundWorker).start(); executed only after new Thread(geneticEngineWorker).start(); will be stopped.
So if I’m right could you please tell me how code can be reorganized to make the work, and if I’m wrong could you point me to my mistakes?

Thanks everyone in advance!


ADDITION

I debuged it and as I thought, backGroundWorker starts working only after geneticEngineWorker ends or user closes dialog(exception in this case);

  • 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-04T04:27:32+00:00Added an answer on June 4, 2026 at 4:27 am

    yes there are two basics ways by using

    SwingWorker, required deepest knowledge about Java Essential Classes inc. Generics too

    and

    Runnable#Thread required only wrapping output to the GUI into invokeLater()

    simple example about no special effort required for idea Runnable#Thread, JProgressBar all in JTable, thats means that code line model.setValueAt(value, row, column); should be wrapped into invokeLater, but in other hands setText() is declared as thread safe, I’d to suggest wrapping that into invokeLater in all cases

    enter image description here enter image description here

    import java.awt.Component;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    public class TableWithProgressBars {
    
        private static final int maximum = 100;
    
        public void createGUI() {
            final JFrame frame = new JFrame("Progressing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Integer[] oneRow = {0, 0, 0, 0};
            String[] headers = {"One", "Two", "Three", "Four"};
            Integer[][] data = {oneRow, oneRow, oneRow, oneRow, oneRow,};
            final DefaultTableModel model = new DefaultTableModel(data, headers);
            final JTable table = new JTable(model);
            table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    Object waiter = new Object();
                    synchronized (waiter) {
                        int rows = model.getRowCount();
                        int columns = model.getColumnCount();
                        Random random = new Random(System.currentTimeMillis());
                        boolean done = false;
                        while (!done) {
                            int row = random.nextInt(rows);
                            int column = random.nextInt(columns);
                            Integer value = (Integer) model.getValueAt(row, column);
                            value++;
                            if (value <= maximum) {
                                model.setValueAt(value, row, column);
                                try {
                                    waiter.wait(15);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            done = true;
                            for (row = 0; row < rows; row++) {
                                for (column = 0; column < columns; column++) {
                                    if (!model.getValueAt(row, column).equals(maximum)) {
                                        done = false;
                                        break;
                                    }
                                }
                                if (!done) {
                                    break;
                                }
                            }
                        }
                        frame.setTitle("All work done");
                    }
                }
            }).start();
        }
    
        public static class ProgressRenderer extends JProgressBar implements TableCellRenderer {
    
            private static final long serialVersionUID = 1L;
    
            public ProgressRenderer(int min, int max) {
                super(min, max);
                this.setStringPainted(true);
            }
    
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                this.setValue((Integer) value);
                return this;
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TableWithProgressBars().createGUI();
                }
            });
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.