Results 1 to 8 of 8
Thread: Big time NOOB
- 01-12-2009, 11:39 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
Big time NOOB
I have some coding experiance but I'm extremely new to java. I started reading tutorials a few days ago and today I started trying some things out. I'm sure I'm missing something really simple here.
OK. so for my 1st program I decided to write a ping program. It's a GUI with 2 text fields and a button. Type the ip in the 1st field, press button and it gives output in second field.
The problem I have is that I wanted it to say it was working while it is waiting for the dead ips to fail. But the line where it prints "Working..." in the second field doesn't work and I can't figure out why.
Any ideas?
BTW, this is just part of the program. I'm using netbeans as my IDE and it of course adds a lot of other stuff to create the window and stuff so that part is not included.Java Code:public void pingCmd() { try { jTextField2.setText("Working..."); boolean status = InetAddress.getByName(jTextField1.getText()).isReachable(3000); if (status == true) { jTextField2.setText("Alive"); } else { jTextField2.setText("Dead"); } } catch (IOException ex) { Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { pingCmd(); }
- 01-13-2009, 02:07 AM #2
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
can put a stacktrace pls ^_^
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 01-13-2009, 02:47 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
Well, I'm not sure how to do a stack trace with netbeans or java but I'm not sure if a trace would show anything. The app doesn't crash and I've found that the line does actually work but not in the way I intended.
If I comment the other two setText commands then it does print "Working..." but only after the isReachable times out. And since the other two setText commands are also printed after it times out then it is overwritten so fast you can't see it.
The 1st line in the main method is:
which from what I've read doesn't appear to be the culprit(unless I'm misunderstanding it).Java Code:java.awt.EventQueue.invokeLater(new Runnable() {
Any other ideas? Or can someone point me in the right direction for doing the stack trace?
Thanks for your help BTW.
- 01-14-2009, 03:36 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
I found the solution to my problem! Woot!
Ok, sorry. Anyway I'll post it here in case anyone else is a huge java noob like me and didn't know this info.
With swing apps when you have some code that is going to take a while (like 2 seconds for something to timeout) and you run the code in the same thread (which is called the event dispatch thread) then your gui will not update.
so here is how I changed that program to use a worker thread:
And now my gui doesn't freeze while it's waiting for the ping to time out. I'm sure it's not the best implementation of SwingWorker or any of the other code either, but it is my 1st real java app and I'm still learning.Java Code:private void pingCmd() { SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { jTextField2.setText("Working..."); boolean status = InetAddress.getByName(jTextField1.getText()).isReachable(3000); if (status == true) { jTextField2.setText("Alive"); } else { jTextField2.setText("Dead"); } return status; } }; worker.execute(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { pingCmd(); }
-
You are on the right track here by performing long actions in a background thread so as not to tie up the Swing Event Dispatch Thread (EDT), but you must take care when doing this that you don't inadvertently cause thread violations. These violations cause some pernicious errors that only happen every now and then, but are a witch to debug.so here is how I changed that program to use a worker thread:
For instance, you need to realize that code run in the doInBackground method is done off of the EDT, and so you shouldn't make Swing calls from within this method without proper precautions. By Swing calls here I mean reading from and especially writing to the JTextFields. Another way to do what you want is to use the done() method to do your writing to text fields and a constructor to do the reading:
Java Code:public class Fubar { private JTextField jTextField1 = new JTextField(10); private JTextField jTextField2 = new JTextField(10); private void pingCmd() { jTextField2.setText("Working..."); // pass the jtextfield text in a thread-safe way Worker worker = new Worker(jTextField1.getText()); worker.execute(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { pingCmd(); } private class Worker extends SwingWorker<Boolean, Void> { private String addressString; Worker(String addressString) { this.addressString = addressString; } @Override // this is not called on the EDT protected Boolean doInBackground() throws UnknownHostException, IOException { boolean status = InetAddress.getByName(addressString).isReachable(3000); return status; } @Override protected void done() // this is called on the EDT { try { if (get()) // get gets what is returned from the doInBackground method, a Boolean { jTextField2.setText("Alive"); } else { jTextField2.setText("Dead"); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }Yeow, you don't start "small" do you? LOL, well, best of luck.but it is my 1st real java app and I'm still learning.
- 01-14-2009, 05:49 PM #6
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
Thank you very much. That info is very useful.
I didn't think about possible problems coming from writing to text fields in the worker thread. But now I can definitely see the reasoning for doing it that way.
-
You're welcome.
I didn't either, but a while back I looked at the source code for this, and it's a lot more involved than I had thought and carries with it a significant risk for thread violations.I didn't think about possible problems coming from writing to text fields in the worker thread. But now I can definitely see the reasoning for doing it that way.
- 01-14-2009, 06:11 PM #8
You were right to create a worker thread; SwingWorker is overkill unless you use its capabilities.
Fubar is correct in saying that you should not update the GUI from a worker thread.
Here is a way to correct your new code without rewriting it. This looks ugly, but it is the standard. You can cut and paste it (or type it from memory)
Your code runs of the event dispatch thread (the Swing thread) after it finishes any previous requests.Java Code:EventQueue.invokeLater(new Runnable() { @Override public void run() { // update your GUI here } });
Similar Threads
-
Noob
By nokomis in forum IntroductionsReplies: 2Last Post: 03-06-2009, 05:10 PM -
Hello, Java Noob
By furry in forum IntroductionsReplies: 0Last Post: 10-07-2008, 07:39 PM -
Please help a noob :)
By Bays in forum New To JavaReplies: 15Last Post: 06-17-2008, 06:11 AM -
[noob]Problem with TOS .bat
By fred33 in forum Advanced JavaReplies: 0Last Post: 03-19-2008, 02:04 PM -
Ah! Help a Java Noob
By Snejana in forum New To JavaReplies: 4Last Post: 01-24-2008, 03:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks