swingworker

Server based SwingWorker does not Stop

巧了我就是萌 提交于 2019-11-29 12:30:24
I have a boolean variable to control the execution of the server (start/stop) : private boolean ecoute=true; here is my class: sw=new SwingWorker<String,Void> (){ protected String doInBackground() throws Exception { try { server = new ServerSocket(Integer.parseInt(port.getText())); String str1="waiting for connexion.."; String str2="Connexion ok"; log.append(str1+"\n"); PrintWriter out=null; BufferedReader in=null; Socket socClient=null; while(ecoute){ socClient = server.accept(); log.append(str2+"\n"); in = new BufferedReader( new InputStreamReader(socClient.getInputStream()) ); out = new

Difference between SwingUtilities.invokeLater and SwingWorker<Void, Object>?

爷,独闯天下 提交于 2019-11-29 11:35:43
What is the difference between: //Some code, takes a bit of time to process (new SomeJFrame()).setVisible(true); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { (new SomeJWindow()).start();//Start a new thread } }); And: class doGraphics extends SwingWorker<Void, Object> { @Override public Void doInBackground() { //Some code, takes a bit of time to process (new SomeJFrame()).setVisible(true); return null; } @Override protected void done() { (new SomeJWindow()).start();//Start a new thread } } (new doGraphics()).execute(); Which method is better to use? SwingUtilities

Pause and Resume SwingWorker.doInBackground()

柔情痞子 提交于 2019-11-29 08:41:56
I have a basic Swing UI with a single button marked "Play." When the button is pressed the label changes to "Pause". When the button is pressed now it changes to say "Resume." On "Play" I am instantiating and executing a SwingWorker. What I would like is to be able to pause this thread (NOT cancel it) and resume it according to the button presses described above. However, I'd prefer not to resort to Thread.sleep() in doInBackground(). That seems a bit hackish. Is there any way for the thread running doInBackground to block? Pause and Resume SwingWorker.doInBackground() First of all you have to

How to return value from SwingWorker class and use in other class and enable MenuItem when process is done?

五迷三道 提交于 2019-11-28 14:42:55
I am using SwingWorker class to make a process run in another thread. What I want is, once this thread has finished processing, it should return a String and also it should enable a JMenuItem . I am using the done() method in the SwingWorker class to enable the JMenuItem but I receive a NullPinterException . The doInBackground() method returns a String which I want to access in the main GUI class - GUIMain.java , present in the same package. How should I do that? I saw many examples which implement done() or onPostExecute() methods, but I think I am going wrong somewhere. Here is the code

getting the cancel event of Java ProgressMonitor

让人想犯罪 __ 提交于 2019-11-28 13:43:35
I have a ProgressMonitor pm and a SwingWorker sw . I want to cancel the SwingWorker when I press the cancel -button on pm . I guess this shouldnt be too hard, and I read some tutorials about SwingWorker and ProgressMonitor, but I can't get this to work. final ProgressMonitor pm = new ProgressMonitor(frame, "checking", "...", 0, 100); final SwingWorker sw = new SwingWorker() { protected Object doInBackground() throws Exception { doSomethingAndUpdateProgress(); } }; sw.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if(evt

Opening JDialog with SwingWorker?

被刻印的时光 ゝ 提交于 2019-11-28 13:13:39
I have a project J2SE that use JPA. In some JDialogs I do returns getResultList() and populate JTable, JComboBox, JList etc. at constructor of class. So when I will create any instance for these dialogs sometimes are slow. I think use SwingWorker and JProgressbar and create a (loading) to open JDialogs is a good solution, but I don't know how to do this. I'm trying this. // JProgressbar progress = new JProgressBar(); //custommer dialog JDialog custommer = new JDialog(); //here slow because I have List<Customer> and others lists custommer.setModal(true); private void openDialogs(JDialog dialog)

JProgressBar not triggering propertyChange on setProgress

家住魔仙堡 提交于 2019-11-28 12:56:52
问题 I've read many different articles about JProgressBar...including the dodgy code found over at Java; here. Most indicate you need a SwingWorker to get things happening properly, which makes perfect sense, I understand that much. I am finding that when I call setProgress(value) to update the progressbar, it's not triggering the propertyChange event most of the time. I've checked the value I'm passing to setProgess and it definitely changes every time, so I'm not sure if it's just firing the

Best practice to start a swing application

徘徊边缘 提交于 2019-11-28 12:26:15
What is the best practice way to start a java swing application? Maybe there is another way to do it. I want to know if i have to use the SwingUtilities class to start the application (secound possibility) or not (first possibility). public class MyFrame extends JFrame { public void createAndShowGUI() { this.setSize(300, 300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // add components and stuff this.setVisible(true); } public static void main(String[] args) { // First possibility MyFrame mf = new MyFrame(); mf.createAndShowGUI(); // Secound possibility SwingUtilities.invokeLater(new

Use of SwingWorker to do complex tasks in background

安稳与你 提交于 2019-11-28 11:42:56
问题 I am having a GUI of login screen. Whenever i press the login button the user name and password is checked against entry in an online mysql database,i'm extracting all this information from database in actionPerformed() method of the login button.Problem is while program is fetching data from database the GUI freezes.I googled my problem and found that i should use SwingWorker but being a newbie i didn't get how to use SwingWorker for my purpose. 回答1: First of all, declare a member variable

How do I wait for a SwingWorker's doInBackground() method?

柔情痞子 提交于 2019-11-28 10:56:26
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test { private JDialog window; public Test { // instantiate window } private class Task extends SwingWorker<Void, Void> { public Void doInBackground() { try { Thread.currentThread().sleep(5000); } catch(InterruptedException e) {} return null; } } public void doTask() { Task task = new Task(); task.execute(); } protected void process() { // update various GUI components here } public static void main(String args[]) { Test t = new Test(); t.doTask(); System.out.println("done"); } }