JButton stays pressed

In my Java GUI application, I have JButton , and when I click on it, it calls function to connect to the database , then calls function until clear a table in DB , then calls function , which reads text from a single file and loads variables , which calls function , which reads text from another file, compares data with both, and then calls function on update or insert data in the database, all this works fine.

However, my question is related to JButton , when I clicked it, I want to run Indeterminate progress bar only so that the user knows that the work is in progress, and then right before he leaves the action listener setIndeterminate to false , and set the value to progress bar to 100(complete) , but in my case, when you press the button , it remains in a click state, and the progress bar freezes.

What should I implement to prevent this? perhaps? but Im brand new for slicing in java. here is my action listener:

  private class buttonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if( e.getSource() == genButton ) { progressBar.setIndeterminate(true); progressBar.setString(null); try { dbConnect(); //connects to DB clearSchedules(); // deletes data in tables readFile(); // reads first file and calls the other functions dbClose();// closes the DB progressBar.setIndeterminate(false); progressBar.setValue(100); } catch (Exception e1){ System.err.println("Error: " + e1.getMessage()); } } } } 

On the side of the note, I would like the action bar to really move as the program progresses, but I was not sure how to control its progress.

Thank you beef.

UPDATE here is my example of SwingWorker and how I used it:

Globally announced

  private functionWorker task; private abstract class functionWorker extends SwingWorker { public void execute() { try { dbConnect(); } catch (SQLException e) { e.printStackTrace(); } clearSchedules(); try { readFile(); } catch (IOException e) { e.printStackTrace(); } dbClose(); } } 

Inside my actionPerformed method

 if( e.getSource() == genButton ) { progressBar.setIndeterminate(true); progressBar.setString(null); try { task.execute(); progressBar.setIndeterminate(false); progressBar.setValue(100); } catch (Exception e1){ System.err.println("Error: " + e1.getMessage()); } } 
+4
source share
4 answers

The problem is probably related to connecting to performing expensive operations in the user interface thread (connecting to a database, reading from a file, calling other functions). In no case should you call code that uses excessive processor time from the user interface thread, since the entire interface cannot act while it is executing your code, and this leads to a "dead" application, while the components remain in a time-costing state operations to completion. You have to execute another thread, do expensive work, and then use SwingUtilities.invokeLater(Runnable doRun) with the passed runnable, in which you would update the progress.

You may experience synchronization issues related to component states, but you can fix them later.

+7
source

Can I create a new thread when the action is executed and call new functions in the thread, or should I execute the threads inside the actual function itself?

You can start SwingWorker from your button handler, as shown. An example related to the Runnable implementation is presented here .

+6
source

One of the transition processing methods is the SwingWorker extension in the class. SwingWorker takes care of performing background tasks for you, and therefore you do not need to implement your own threads, which may be in unknown problems. To begin with, your class that takes care of the execution user interface must implement PropertyChangeListener and implement public void propertyChange(PropertyChangeEvent evt) { - to update the progressbar state based on the global variable.

The background task class should look like this (it could be an inner class):

 class ProgressTask extends SwingWorker<Void, Void> { @Override public Void doInBackground() { //handle your tasks here //update global variable to indicate task status. } @Override public void done() { //re-enabled your button } } 

on the button event listener:

  public void actionPerformed(ActionEvent evt) { //disable your button //Create new instance of "ProgressTask" //make the task listen to progress changes by task.addPropertyChangeListener(this); //calll task.execute(); } 

I tried using a sample code, you would need to read some tutorial to understand how all these parts fit together. However, the main thing is not to encode your streams, use SwingWorker

+5
source
  progressBar.setIndeterminate(true); progressBar.setValue(0); dbConnect(); //connects to DB progressBar.setIndeterminate(true); progressBar.setValue(10); clearSchedules(); // deletes data in tables progressBar.setIndeterminate(true); progressBar.setValue(50); readFile(); // reads first file and calls the other functions progressBar.setIndeterminate(true); progressBar.setValue(75); dbClose();// closes the DB progressBar.setIndeterminate(false); progressBar.setValue(100); 

You need to indicate an indicator of progress, how much has been done, because it does not know how many percent has been completed. Better yet, write a method that updates and reconstructs the progress bar, rather than repeating method calls here.

 updateProgressBar(int progress, boolean isDeterminate, String msg){}; 

You also need to make sure that your specific button activates the performed action.

 class IvjEventHandler implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent e) { if (e.getSource() == JMyPanel.this.getJButtonUpdate()) connEtoC1(e); }; }; 

Connect C1 (e); should execute a controller class or SwingWorker, and not run from a GUI

0
source

All Articles