swingworker

java SwingWorker.doInBackground() must not access GUI elements

夙愿已清 提交于 2019-12-05 05:44:18
May be this is trivial, I am struggling to understand a simple documentation on SwingWorker . Here is the copy pasted content Workflow There are three threads involved in the life cycle of a SwingWorker : Current thread: The execute() method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately. One can wait for the SwingWorker to complete using the get methods. Worker thread: The doInBackground() method is called on this thread. This is where all background activities should happen. To notify PropertyChangeListeners about bound

How can this SwingWorker code be made testable

夙愿已清 提交于 2019-12-04 16:24:29
问题 Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private String location = url.getText(); @Override protected File doInBackground() throws Exception { File file = new File("out.txt"); Writer writer = null; try { writer = new FileWriter(file); creator.write(location, writer); } finally { if (writer != null) { writer.close(); } } return file; } @Override protected void done() { setEnabled(true); try { File file = get();

SecondaryLoop instead of SwingWorker?

≡放荡痞女 提交于 2019-12-04 13:27:52
问题 From the documentation for SecondaryLoop, it is not clear when you should use this new feature instead of a SwingWorker, a few examples of interesting cases would be useful. 回答1: The intent of SecondaryLoop is basically the same as SwingWorker. However, using SecondaryLoop can be less hairy than chaining together multiple SwingWorkers. Take a look at Hidden Java 7 Features – SecondaryLoop for a detailed explanation & example. 来源: https://stackoverflow.com/questions/10196809/secondaryloop

Update UI using swingworker thread

你说的曾经没有我的故事 提交于 2019-12-04 09:51:35
I want to use the swing worker thread to update my GUI in swing. pls any help is appreciated.I need to update only the status of 1 field using the thread i.e setText(). Eng.Fouad I just answer similar question on another forum for a question about SwingWorker: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing

Should i use SwingUtilities.invokeLater() inside of SwingWorker.doInBackground()?

别说谁变了你拦得住时间么 提交于 2019-12-04 08:06:17
The common way to interact with EDT from swing worker is useing get() method. But i have a long task and code like this: public Void doInBackground() { for(Object o : objects) { doSomething(); MyGlobalGUIConsole.addMessage("Done for " + o); } } In most tutotials is recommended to use return values to get something back from SwingWorker to EDT, but can i just: public Void doInBackground() { for(Object o : objects) { doSomething(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { MyGlobalGUIConsole.addMessage("Done for " + o); } }); } } You can, but the SwingWorker has

Java SwingWorker not terminating on task completion

十年热恋 提交于 2019-12-04 05:45:54
问题 Ok, so I've been playing around with SwingWorker a bit and got some simplified code for updating a gui going, but I'm having trouble figuring out how to get the thread to properly terminate when it completes. Currently, it is only terminating via the stop option. How would I set it up to also terminate the thread properly when it finishes its process? Currently, after the return null; it goes to the package line and hangs. My code is below: package concurrency; import java.util.List; import

Swingworker with FileVisitor class and walkFileTree(), which iterates internally over a “set” of files in a directory tree; where to publish()?

£可爱£侵袭症+ 提交于 2019-12-04 05:33:46
问题 It seems like the long-running tree walker task should be defined in a class like so: public class TreeWalker extends SwingWorker<Void,String> implements FileVisitor<Path> And begun somewhere like so: TreeWalker walker = (new TreeWalker()); walker.execute(); The long-running task is not only initiated by but completely carried out by a single call to walkFileTree() , a method in the Files class. So surely the call to it has to be in doInBackGround() . protected Void doInBackground() throws

Changing the default cursor to busy cursor does not work as expected

房东的猫 提交于 2019-12-04 04:58:16
问题 After many attempts trying to make a JProgressBar work as expected, I finally became successful at achieving my goal. I had used @MadProgrammer's advice and used a SwingWorker to finally get the program work as I want. Now, I want the cursor to change into when my JProgressBar goes from 0% to 100%. I've googled and found out that setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); is the code to do it. I've tried it but it does not work as expected. Relevant piece of code: JProgressBar

setvisible method in java swing hangs system

♀尐吖头ヾ 提交于 2019-12-04 03:19:13
问题 I have banking gui application that I am currently working on and there seems to be a problem with the setvisible method for my jdialog. After the user has withdrawn a valid amount I pop up a simple dialog that says "transaction in progress". In my dobackground method i keep polling to check if the transaction has been received. I tried using swingworker and I don't understand why it's not working. If i remove the setvisible call it works fine, so why does setvisible cause the system to hang?

Swing: Can't get JButton to update - repaint() not working

假装没事ソ 提交于 2019-12-04 01:18:47
问题 I'm using Swing for the first time to create a simple GUI. It consists of a JFrame upon which I have placed a single JButton which, when clicked, calls some other code which takes approx. 3 seconds to return. Just before the call to this code, in actionPerformed() , I want to update the text on the button to inform the user that processing is occuring. My problem is that the text on the button does not update until after the 3-second call has returned. I want the updated text to be present