swingworker

Java slideshow image delay using paintComponent

删除回忆录丶 提交于 2019-12-01 05:32:20
问题 I am putting together a slideshow program that will measure a user's time spent on each slide. The slideshow goes through several different magic tricks. Each trick is shown twice. Interim images are shown between the repetition. Transition images are shown between each trick. On the first repetition of a trick the JPanel color flashes on the screen after a click before the next image is shown. This doesn't happen during the second repetition of the same trick. It's possible that the image is

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

若如初见. 提交于 2019-12-01 04:04:40
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 during the call, then I'll change it back afterwards. Calling repaint() on the JButton doesn't do

Waiting for a cancelled future to actually finish

蓝咒 提交于 2019-12-01 02:47:05
问题 I have a SwingWorker which calls some code that does not check for thread interruption. After the call to worker.cancel(true) , the worker.get() method will throw CancellationException immediately (as it is supposed to). However, since the background task's code never checks for its thread to be interrupted, it happily continues executing. Is there a standard way to wait for the background task to actually finish? I'm looking to show a "Cancelling..." message or something of the sort and

Problem making a JProgressBar update values in Loop (Threaded)

风流意气都作罢 提交于 2019-11-30 21:41:02
Am trying to get my program to update the progress bar values constantly within a method while performing some operations. However this does not happen until the end, and the UI freezes. After looking around to similar questions with my problems, I tried to implement the accepted solutions (Using threads) however I cannot get it to work correctly. Is just like if they where not there. My program contains several classes, the Main being the one automatically created by netbeans on the JFrame Design mode, so there are certain things such as the static void main and the public Main that am not

Using SwingWorker and Timer to display time on a label?

本小妞迷上赌 提交于 2019-11-30 21:06:53
I want to have a clock showing current time and refreshes every second. The code I am using is: int timeDelay = 1000; ActionListener time; time = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { timeLabel.setText(DateTimeUtil.getTime()); /*timeLabel is a JLabel to display time, getTime() is samll static methos to return formatted String of current time */ } }; SwingWorker timeWorker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { new Timer(timeDelay, time).start(); return null; } }; timeWorker.execute(); What I want to refresh

Why SwingWorker? Why not just Thread or Runnable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:40:21
What are the advantages of using SwingWorker instead of Thread or Runnable ? Thread and Runnable were part of Java 1.0; they're as good as they were back then. The new concurrency classes distill all that's been learned about multi-threading since then (thank you, Doug Lea and others). Writing multi-threaded code is terribly difficult. The new concurrency classes, including SwingWorker, try to make that easier. Start by noting the generics for strong typing. There's a mechanism built in to publish and process both final and intermediate results. It'd be possible to mimic these with Thread and

Turn-based Game Design: Event-Driven vs. Game Loop

梦想的初衷 提交于 2019-11-30 13:26:49
问题 I am creating my first game in Java. The game is Monopoly. I am struggling with how I should design the game to model its turn-based structure (managing player turns). I want to allow for both a single human-controlled and one or multiple AI-controlled players to play the game. My specific issue is that I do not know whether to implement a game loop or not, meaning a loop which can manage the players and the variables directly related to the game of Monopoly, (think of things such as

How to delegate SwingWorker's publish to other methods

我的梦境 提交于 2019-11-30 10:00:27
My "problem" can be described by the following. Assume we have an intensive process that we want to have running in the background and have it update a Swing JProgress bar. The solution is easy: import java.util.List; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.SwingWorker; /** * @author Savvas Dalkitsis */ public class Test { public static void main(String[] args) { final JProgressBar progressBar = new JProgressBar(0,99); SwingWorker<Void, Integer> w = new SwingWorker<Void, Integer>(){ @Override protected void process(List<Integer> chunks) { progressBar

java - execute each SwingWorker class one after another

本小妞迷上赌 提交于 2019-11-30 09:16:24
问题 I have multiple classes extending the SwingWorker. What I wish to accomplish is to execute each class one after another (without executing the next class in the previous class' done method). For example, lets say I have: ClassSwingW1 csw1 = new ClassSwingW1(); csw1.execute; ClassSwingW2 csw2 = new ClassSwingW2(); csw2.execute; ClassSwingW3 csw3 = new ClassSwingW3(); csw3.execute; and etc. public class ClassSwingW1 extends SwingWorker<Void, Void> { @Override protected Void doInBackground()

How I append text to textarea with using swingworker class?

徘徊边缘 提交于 2019-11-30 09:11:38
问题 I want to append text in loop with swingworkerclass in java. for ex : while(true) { mytextarea.append("sometext"); if(some_condition) {break;} } I want this with swingworker because I want to see every uptade on textarea. For this code I only see update when my proccess done. I dont want swingworker samples for another situations. Please give me some code here.Thanks. 回答1: SwingWorker is not right here. Your code is not running in the EDT so you doesn´t see updates. You can use SwingUtilities