Results 1 to 17 of 17
Thread: SwingWorker question
- 03-12-2010, 10:34 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
SwingWorker question
I am a novice-java programmer and currently I am developing a graphical swing interface (GUI.java) for a console-based algorithm (algorithm.java).
In my graphical interface, when I click on the "run algorithm" button, the algorithm is executed and the results which previously were written in the console, now they are written in a text area inside the GUI. The problem is that until the computation is not finished (all results are printed) the textarea is not updated, and the GUI gets irresponsible.
I know I have to do it with a Swing Worker but in all the examples that I found the swing workers run on the same class and therefore is easier
This is a part of code (algorithm.java) which displays the printed results of the algorithm into the textarea:
Help is appreciated.Java Code:for (;;) { System.step(); if (step % SCREEN_INTERVAL == 0) { step++; // output = output of program (each LiNE) output = String.format( "%d\tA=%d\tBt=%d\tRsp=%d\tE=%d\t+=%d\t-=%d\tdr=%d\tF=%d\tFF=%d\t\tB=%d\tLV=%3.2f\n",step - 1, System.gC(), Stats.b, Stats.r, NS.gEC(), Stats.a, Stats.r, Stats.dr, Stats.f, Stats.ff, Stats.b, Stats.l()); // output before the gui // System.out.println(output); // Write into a output file (pw = PrintWriter) pw.println(output); // write output into the gui (TEXTAREA) GUI.jtxaConsole.append(output); Stats.reset(); } else step++; }
Thanks!
-
If the code above is in the SwingWorker's doInBackground method, then it suggests that you're trying to update a Swing component directly from the background thread, which is not what you want to be doing. Are you using the SwingWorker's publish/process methods? If not you should as these would allow you to do what you want easily. The Sun tutorial on this can show you the way.
You also look to be doing things here in a static and not OOPs way. If so, you may wish to correct this. Best of luck!
- 03-12-2010, 02:24 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Could you illustrate me with an example?
Thank you in advance
-
Again, the Sun tutorials have explanation and examples. Have you Googled for this and looked at them?
If not: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
-
Heck, a simple example:
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.*; public class FuSwing { private static void createAndShowGUI() { final FuSwingGui gui = new FuSwingGui(); gui.doItAddActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { gui.start(); new MySwingWorker(gui).execute(); } }); JFrame frame = new JFrame("FuSwing Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(gui.getMainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } class FuSwingGui { private JPanel mainPanel = new JPanel(); private JTextArea textarea = new JTextArea(15, 20); private JButton doItButton = new JButton("Do It!"); public FuSwingGui() { textarea.setEditable(false); textarea.setFocusable(false); mainPanel.add(new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); mainPanel.add(doItButton); } public void doItAddActionListener(ActionListener al) { doItButton.addActionListener(al); } public void appendToTextArea(String text) { textarea.append(text); } public void start() { doItButton.setEnabled(false); } public void done() { doItButton.setEnabled(true); } public JPanel getMainPanel() { return mainPanel; } } class MySwingWorker extends SwingWorker<Void, String> { private FuSwingGui gui; public MySwingWorker(FuSwingGui gui) { this.gui = gui; } @Override protected Void doInBackground() throws Exception { for (int i = 0; i < 20; i++) { String text = "From Swing Worker: " + (i + 1); publish(text); System.out.println(text); Thread.sleep(500); } return null; } @Override protected void process(List<String> chunks) { for (String text : chunks) { gui.appendToTextArea(text + "\n"); } } @Override protected void done() { gui.done(); } }
- 03-18-2010, 03:41 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
I have read the tutorials, my problems is that I created the application with the netbeans assistant and there is no such method createandshowgui() in the gui;besides the operation to do in background is a different algorithm in another package and different class. so I dont know where to put the do in background method.
Anyway in the example you pasted me when i comment the lines:
Application works in the same way, I would be interested in the same piece of code but without the swingworker code, to check where I should put the methods.Java Code:javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run()
Thanks and sorry but I am really newbie
-
I don't understand what you're asking here. Please outline anything specific that you don't understand about my post.
While I do think that it's fine to use NetBeans as an IDE, at this stage you should not be using NetBeans to generate your GUI code. Rather you should be learning to create it yourself by hand since if you did, I think that you'd understand the code that I posted.
- 03-18-2010, 05:30 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
I understand the code you posted, but realize swingworker in my case is more complicated.
Please, I would like the same example, but without the swingworker, so I can see how and where you added the different methods and where and how the concurrency works.
ignore what i said about netbeans.
-
I'm sorry, but I'm still not getting you. To see my code without the SwingWorker, simply get rid of the MySwingWorker class and comment out the use of this class in the action listener:
Java Code:gui.doItAddActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { gui.start(); // new MySwingWorker(gui).execute(); } });
I still think that for us to help you better you need to write out a well-formed paragraph about just what's not working for you. Also look into creating and posting an SSCCE or "Short, Self Contained, Correct (Compilable), Example" so we can see a small portion of your code that well illustrates your current problem.
- 03-19-2010, 02:56 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Another try:
I have graphical swing interface (GUI.java) for a console-based algorithm (algorithm.java). Inside the constructor of algorithm.java there is a method execute() which do some computations and prints them into a text area.
In my graphical interface, when I click on the "run algorithm" button, the algorithm.java is executed. The problem is that until the computation is finished the GUI gets irresponsible.
I implemented the Swingworker in my algorithm.java:
It does not solve my problem.Java Code:public class Algorithm extends SwingWorker { public Algorithm{ ... this.doInBackground(); } public void execute(){ /* here they go computations */ ... @Override protected Object doInBackground() throws Exception { this.execute(); } }
in my GUI.java in the actionperformed of the "Run Algorithm" Button
Help?Java Code:public class GUI extends FrameView { ... private void goButtonActionPerformed(java.awt.event.ActionEvent evt) { Algorithm algo = new Algorithm(arrayArgum, outputpath, this); algo.execute(); } }Last edited by cotarelo; 03-19-2010 at 03:30 PM.
-
What's this about?
Java Code:public Algorithm{ ... this.doInBackground(); // *** holy mackerel!?! *** }
This will mess you up royally and cause all of the background stuff to be called on the EDT. So don't do this. In fact, there's a good chance that this may be the source of your problems.
If you still are having problems, then again, create an SSCCE and post it. The link is in my post above.Last edited by Fubarable; 03-19-2010 at 07:46 PM.
- 03-22-2010, 09:24 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Thanks for the reply, my code was very messy and I have some mistakes. Now I am getting closer, as the multi threading seems to work, but, still I can not do what I want.
This is my code in GUI.java
For some reason, in the Doinbackground method, I receive the "From Swing worker" text on the textarea, but the call to the algorithm is never done, and I do not receive the results of the computations in the textarea. Of course without the swingworker the call to the algorithm works.Java Code:... private void goButtonActionPerformed(java.awt.event.ActionEvent evt) { goButton.setEnabled(false); // An additional line. computeTask = new ComputeTask(this); computeTask.execute(); } ... private class ComputeTask extends SwingWorker{ ... GUI view; ... private ComputeTask(GUI v) { view = v; } .. @Override protected Void doInBackground() throws IOException { Algorithm algo = new Algorithm(arrayArgum, outputpath, view); for (int i = 0; i < 2000000; i++) { String text = "From Swing Worker: " + (i + 1)+"\n"; textarea.append(text); } goButton.setEnabled(true); return null; } }
-
I have requested several times for an SSCCE. If you are serious about needing help, then it would be in your best interest to make the effort to read the link about this, create one, and post it. Otherwise I only have partial information, and certainly not enough to be able offer advice. Up to you.
Best of luck.
- 03-22-2010, 04:51 PM #14
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
My only problem is that this call inside the Doinbackgound method.
Seems to never be done. The algorithm should answer in my textarea with results (if I place the call outside the the swingworker method it does it correctly).Java Code:Algorithm algo = new Algorithm(arrayArgum, outputpath, view);
However, the text From Swing Worker: 1
From Swing Worker: 2.
...
It does it correcly, and I can move the scroll bar in the textarea when it is still running
-
- 03-23-2010, 09:05 AM #16
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
The view was redeclared
And that is why it was not passing the parameters.Java Code:GUI view;
PS: I tried to do a SSCCE but I did not know how, the page is very unclear, and sure an poster does not want to loose time on this. maybe a tutorial in this forum would work
-
and can you see why there's no way we can see that this was your problem given what you've posted above? An SSCCE would have shown this to us days ago.
It's a pretty straight forward process of trying to isolate your problem into a small compilable program that we can run and view. Why don't you let us know what parts about the page are confusing and we can help you with it. Otherwise we will all continue to be frustrated by your posting snippets of code that don't contain the actual problem, and that we can't run or test. Thus if you had posted a proper SSCCE in the first place, you'd likely have had your solution days ago and would have saved time,... but it's your dollar.PS: I tried to do a SSCCE but I did not know how, the page is very unclear, and sure an poster does not want to loose time on this. maybe a tutorial in this forum would work
Similar Threads
-
SwingWorker Problem
By Berkan in forum Threads and SynchronizationReplies: 10Last Post: 03-11-2010, 03:28 AM -
SwingWorker problem. Works in Vista but not in Linux.
By pellebye in forum New To JavaReplies: 18Last Post: 10-13-2009, 03:00 PM -
SwingWorker Opinions
By frejon26 in forum AWT / SwingReplies: 3Last Post: 04-13-2009, 08:41 PM -
SwingWorker problem!!! How can I run it 2 times or more?
By davigre in forum AWT / SwingReplies: 3Last Post: 10-02-2008, 05:48 AM -
swingworker
By musiigedeo in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 12:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks