private void clearTable(){ SwingWorker clearTable = new SwingWorker() { @Override public Object construct() { return null; } @Override public void finished() { roomsTable.clearTable(); } }; clearTable.start(); }
SwingWorker worker = new SwingWorker() { @Override public Object construct() { // add your code here progressBar.setVisible(true); progressBar.setIndeterminate(true); // and so on... return 0; } }; worker.start();
public void run() { finished(); } };
/** * Start a thread that will call the <code>construct</code> method * and then exit. */ public SwingWorker() { new Runnable() { public void run() { finished(); } }; Runnable doConstruct = () -> { try { setValue(construct()); } catch ( Exception e ) { Log.error( e ); } finally { threadVar.clear(); } SwingUtilities.invokeLater( () -> finished() ); }; Thread t = new Thread(doConstruct); threadVar = new ThreadVar(t); }
SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { // Done some work... } @Override protected void done() { // stop the animation... } };
/** * Return the value created by the <code>construct</code> method. * Returns null if either the constructing thread or the current * thread was interrupted before a value was produced. * * @return the value created by the <code>construct</code> method */ public Object get() { while (true) { Thread t = threadVar.get(); if (t == null) { return getValue(); } try { t.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // propagate return null; } } }
SwingWorker worker = new SwingWorker() { public Object construct() { ... //add the code for the background thread } public void finished() { ... //code that you add here will run in the UI thread } };
public void remove() { SwingWorker worker = new SwingWorker() { @Override public Object construct() { return 42; } @Override public void finished() { _commandpanel.remove(_icon); } }; worker.start(); }
SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { // TODO Auto-generated method stub // open dialog here return null; } };
private void addRosterEntry(final BareJid jid, final String nickname, final String group) { final SwingWorker rosterEntryThread = new SwingWorker() { public Object construct() { return addEntry(jid, nickname, group); } public void finished() { if (get() == null) { UIManager.put("OptionPane.okButtonText", Res.getString("ok")); JOptionPane.showMessageDialog(dialog, Res.getString("label.unable.to.add.contact"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); } } }; rosterEntryThread.start(); }
worker = new SwingWorker() { public Object construct() {
public void search(final String query) { SwingWorker worker = new SwingWorker() { public Object construct() { // On initialization, find search service. if (searchServices == null) { loadSearchServices(); } return true; } public void finished() { processQuery(query); } }; worker.start(); }
static void disable(final AbstractButton b, final long ms) { b.setEnabled(false); new SwingWorker() { @Override protected Object doInBackground() throws Exception { Thread.sleep(ms); return null; } @Override protected void done() { b.setEnabled(true); } }.execute(); }
public void actionPerformed(ActionEvent evt) { SwingWorker worker = new SwingWorker() { @Override public Object construct() { return 42; } @Override public void finished() { SparkManager.getUserManager() .searchContacts("", SparkManager.getChatManager() .getChatContainer().getChatFrame()); } }; worker.start(); } });
public void actionPerformed(ActionEvent e) { SwingWorker worker = new SwingWorker() {
/** * Retrieve private notes from server. */ private void retrieveNotes() { // Retrieve private notes from server. final SwingWorker notesWorker = new SwingWorker() { public Object construct() { return PrivateNotes.getPrivateNotes(); } public void finished() { final PrivateNotes privateNotes = (PrivateNotes)get(); showPrivateNotes(privateNotes); } }; notesWorker.start(); }
SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception {
private void stopLoadingImg(){ SwingWorker stopLoading = new SwingWorker() { @Override public Object construct() { return null; } public void finished() { refreshButton.setIcon(SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); refreshButton.validate(); refreshButton.repaint(); refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); refreshItem.validate(); refreshItem.repaint(); } }; stopLoading.start(); }
public void mousePressed(MouseEvent mouseEvent) { final SwingWorker closeTimerThread = new SwingWorker() { public Object construct() { try { Thread.sleep(100); } catch (InterruptedException e) { Log.error(e); } return true; } public void finished() { close(sparktab); } }; closeTimerThread.start(); } });
private void startLoadingImg(){ SwingWorker startLoading = new SwingWorker() { @Override public Object construct() { return null; } @Override public void finished() { refreshButton.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE)); refreshButton.validate(); refreshButton.repaint(); refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE)); refreshItem.validate(); refreshItem.repaint(); } }; startLoading.start(); }