0

I am trying to get a progress bar to load after a button has been clicked the button is in another class and the swing worker is a class within a class. Here is some code

    public class BigramProgbarTest extends JFrame
    {
        //Create the connection to Neo4j


        //MAIN METHOD
        public static void main(String[] args)
        {

            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    //INITIALIZE JFRAME FORM
                    BigramProgbarTest form=new BigramProgbarTest();
                    form.setVisible(true);
                }
            });

        }
    private static JButton btn;
        //CONSTRUCTOR
        public BigramProgbarTest()
        {

            //the form
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(200,200,800,300);
            setTitle("Netword Data Table");
            getContentPane().setLayout(null);

            //ADD SCROLLPANE
            JScrollPane scroll=new JScrollPane();
            scroll.setBounds(70,80,600,200);
            getContentPane().add(scroll);

            //THE TABLE
            final JTable table=new JTable();
            scroll.setViewportView(table);


            //THE MODEL OF THE TABLE
            DefaultTableModel model=new DefaultTableModel()
            {

                    }
                }
            };
            //Create and run the query here for table



            //ASSIGN THE MODEL TO TABLE
            table.setModel(model);

            model.addColumn("Select"); //Column for check boxes
            model.addColumn("Bigrams"); //Column for Bigrams

            //A while loop here 

            }   //ENDWHILE      

            //OBTAIN SELECTED ROW

        //  BigramProgbarTest fr = new BigramProgbarTest();
                JButton btn=new JButton("Get Selected");

                btn.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent arg0) 
                    {//A method for the button to do something with seleted rows
                    }
}
}
});

    //ADD BUTTON TO FORM
    btn.setBounds(71,39,130,30);
    getContentPane().add(btn);

So here you can see that the class has tables and a scroll pane (just showed them in case they have an impact). The I have a swing worker class and progress bar in another class within this class. Here is some code from the swingworker class.

        JProgressBar progressBar = new JProgressBar();
            progressBar.setBounds(211, 42, 146, 14);
            getContentPane().add(progressBar);
            ProgressWorker worker = new ProgressWorker(progressBar);
            worker.execute();

        }


        class ProgressWorker extends SwingWorker<Void, Integer> {
            private final JProgressBar progress;

            public ProgressWorker(JProgressBar progress) {
                this.progress = progress;
            }

            //btn.addActionListener(new ActionListener() //Trying to find somewhere to use this 
            @Override
            protected Void doInBackground() throws Exception  { //This should be done after a button click

//Some code and for loop for the progress bar

    final int progr = (int) ((100L * (results.length - i2)) / results.length);
                publish(progr);

                }//ENDFOR each record// |  | ENDFOR each record//

                }

            return null;
        }
        //final int progr = ((int) ((100L * (recordLoop - firstRecord)) / (lastRecord-firstRecord)));

        @Override
        protected void process(List<Integer> chunks) {
            progress.setValue(chunks.get(chunks.size() - 1));
            super.process(chunks); //This is the process of how the progress bar will load 
        }

        @Override
        protected void done() {
            progress.setValue(100); //This is the value when process is complete  
        }
    }
    }

Now what I expect this to do is run the swingworker class after the button has been clicked.

At the moment the progress bar just loads to right to the end before clicking anything and even before I even select a checkbox. So I want the swingworker to be able to listen to the button click from the main class before performing any tasks.

David Gardener
  • 93
  • 2
  • 13
  • https://stackoverflow.com/questions/23699256/using-swingworker-to-update-the-progress-of-a-thread-from-another-class https://stackoverflow.com/questions/29149798/access-java-jframe-from-another-class check this links – Suresh Kumar Dec 07 '17 at 10:39
  • It "looks" like your executing the `ProgressWorker` in `BigramProgbarTest` constructor, when it should be executed from within the buttons `ActionListener` – MadProgrammer Dec 07 '17 at 11:53
  • I have tried adding it to the action listener but I get an error with ProgressWorker worker = new ProgressWorker(progressBar); worker.execute(); – David Gardener Dec 07 '17 at 12:08
  • Actually I have tried this again a bit differently. So now it doesn't give me an error but the problem I have now is that the progress bar is not loading even after clicking the button. – David Gardener Dec 07 '17 at 12:17
  • and I know the progress bar works because I have tested it by running it on it's own and it loads fine but when I try to add it to the button I get nothing – David Gardener Dec 07 '17 at 12:18
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 07 '17 at 13:07

0 Answers0