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.