swingworker

Execute only one instance of SwingWorker

ε祈祈猫儿з 提交于 2019-12-25 07:43:27
问题 Suppose I have a Java GUI which shows in a panel 40 Batch objects from a selected zone which can go from A to Z. The 40 Batch objects are queried from database which caches them by zone so that each request for a zone doesn't involve the database every time. public class BatchView { private int drawIt(Graphics g, String zone) { for(int i = 0; i<40; i++) { Batch tmpBatch = BatchDAO.getBatch(zone, i); //draw the tmpBatch object } } } public class BatchDAO { public static MyCache cache = new

How to stop SwingWorker.doInBackground process immediately?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 06:09:09
问题 According to Oracle docs SwingWorker.doInBackground does not include any code that might throw InterruptedException ". Dunno if I'm right about what I understand, but I noticed that cancelling the doInBackground process prints out the InterruptedException so it'll continue even if an Exception is encountered? Then how can we cancel and stop the process immediately? So I tried return STATUS; when it is cancelled but still continues. I also tried PROCESS.destroyForcibly() before return but

JFrame only shows components at first creation

不想你离开。 提交于 2019-12-25 02:37:09
问题 When I start my application it opens a JFrame (the main window) and a JFilechooser to select an input directory, which is then scanned. The scan method itself creates a new JFrame which contains a JButton and a JProgressBar and starts a new Thread which scans the selected Directory. Up until this point everything works fine. Now I change the Directory Path in my Main Window, which calls the scan method again. This time it creates another JFrame which should contain the JProgressBar and the

How to wake up waiting SwingWorker?

独自空忆成欢 提交于 2019-12-24 21:10:02
问题 I have a SwingWorker that is running in the background. After it works on some stuff. It will wait. When the user push a button, the MyListener will be invoked and wake up the worker to continue do some stuff. It is not working like I thought it would. It throws an exception at the bottom. Please tell me what I did wrong. MyWorker worker = new MyWorker(); class MyListener implements ActionListener { @Override public void actionPerformed (ActionEvent e) { //DO some stuff and wake up worker

in swing worker propertychange isn't being called

假装没事ソ 提交于 2019-12-24 17:17:23
问题 I have code which I have been using for years and this morning I noticed property change isn't being called when the task is done. I've got the swing worker set up as an inner class and I put a break point on the String properyName = evt..... and it never hits the break point. void loadData() { work2 = new bkgdLoadData(); work2.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName();

Is SwingWorker the only way?

六眼飞鱼酱① 提交于 2019-12-24 16:19:02
问题 I have found that in order to keep Java GUIs (using Swing) responsive the only way is to use the SwingWorker class, as opposed to java.lang.Thread. Is SwingWorker truly the only way when it comes multithreaded GUI based desktop apps? Are there any good alternatives? Rarely can I configure the Thread class to do what I want to do, but SwingWorker usually works, if sometimes in a cumbersome way. 回答1: SwingWorker is nothing but a thin convenience API around Thread . Therefore it is definitely

Implement a Execution Log in Java

徘徊边缘 提交于 2019-12-24 12:24:46
问题 Coming from a WPF world, I am having a hard time implementing some basic stuff in Java. I need to execute a *.JAR file from another java GUI application. This JAR file takes minutes to finish, and I want the GUI not to freeze. So, my initial solution would be: create a SwingWorker class to call a command line in background. My main concerns are: I need to get the console output from the *.JAR file and show it in the GUI. Is it possible to update the GUI from a background thread? Is it

Swing app crashes if minimised

夙愿已清 提交于 2019-12-24 09:27:11
问题 I have a problem in my swing app. I have a SwingWorker that has a for loop inside, that launches 300+ oracle database requests and populates several JTable s with the results. If I keep the swing app window not-minimised, or at least partly visible in the windows explorer, the batch completes fine. Now, if I minimise the app, and then go back to the swing app, it will be frozen. Basic outline and colours of the components will be visible, most of the window will be just of the background

How do I call java.sql.Connection::abort?

被刻印的时光 ゝ 提交于 2019-12-24 08:52:44
问题 I would like to write a SwingWorker that, when the user cancels the task, it: 1) cancels the PreparedStatement and 2) aborts the Connection . Here is some code to illustrate: class Task extends SwingWorker<Void, Void> { Connection conn; PreparedStatement p_stmt; @Override protected Void doInBackground() throws Exception { ResultSet results = p_stmt.executeQuery(); while(results.next()) { if(isCancelled()) return; } return null; } void cancell() { cancel(true); try { p_stmt.cancel(); conn

SwingUtilities.invokeLater()

烂漫一生 提交于 2019-12-24 04:32:19
问题 How do I feel the essentialness of SwingUtilities.invokeLater() in any swing application.Please give some code example. 回答1: Whenever you need to update something inside your GUI you should do it through the AWT Event Thread . This because AWT (and Swing on top) has its own thread that manages everything of a GUI. Without it the graphical interface couldn't handle events and similar things in an asynchronous way while your program is doing something else. So for example if you have a long