Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2007

Tip: Looking for answers? Try searching our database.

got a problem with jtextfiled..

Thread view: 
jlc488 - 25 Nov 2007 04:34 GMT
let's say....i have a jtextfiled ....

JTextField txtNo = new JTextField();

for( int i =0; i<100000; i++){
    //Thread.sleep(100);
    txtNo.setText(i+"");

}

when i'm running it....i want to see the number increasing...but all i
see is...the last result of 99999

it just hangs a while and suddenly appears the last number only...

i want to see flicking..is there anyway????

thanks for reading....
hiwa - 25 Nov 2007 05:05 GMT
> let's say....i have a jtextfiled ....
>
[quoted text clipped - 14 lines]
>
> thanks for reading....

See http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
Swing runs in a single thread in which you do a long task that
prevents Swing to run.
Lew - 25 Nov 2007 05:31 GMT
>> let's say....i have a jtextfiled ....
>>
[quoted text clipped - 17 lines]
> See http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
> Swing runs in a single thread

called the "Event Dispatch Thread", or EDT,

> in which you do a long task that prevents Swing to run.

And if you apply changes from a different thread it might not update visibly
in the EDT.  The link hiwa gave will give you a beginning.

<http://mindprod.com/jgloss/swing.html>
is another good source of information.

Signature

Lew

jlc488 - 25 Nov 2007 07:23 GMT
> >> let's say....i have a jtextfiled ....
>
[quoted text clipped - 32 lines]
>
> - 따온 텍스트 보기 -

i'm not quite understanding..with edt...

i've tried with AWT...this setText is good....

it shows what i want to see...but why not in Swing??

and is there any sample code you can show to me??

thanks..a lot
hiwa - 25 Nov 2007 10:16 GMT
> > >> let's say....i have a jtextfiled ....
>
[quoted text clipped - 42 lines]
>
> thanks..a lot

Wa, wa, wa, wait! Some of JTextComponent methods are thread safe. Your
original code should run normally as is. Try this:
------------------------------------------
import java.awt.*;
import javax.swing.*;

public class SwingTf{

 public static void main(String[] args) throws Exception{

   JFrame frame = new JFrame();
   JTextField tf = new JTextField(30);
   frame.getContentPane().add(tf, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);

   for( int i =0; i<100000; i++){
//     Thread.sleep(100);
    tf.setText(i + "");
   }
 }
}
------------------------------------------
Andrew Thompson - 25 Nov 2007 11:01 GMT
>> > >> let's say....i have a jtextfiled ....
...
>Wa, wa, wa, wait! Some of JTextComponent methods are thread safe. Your
>original code should run normally as is. Try this:

Good point.  OTOH, I think this problem *might* be
better suited to a progress bar (which would require
accounting for the 'update on EDT').  Try doing this
using AWT!

<sscce>
import java.awt.BorderLayout;
import javax.swing.*;

public class SwingProgress {

public static void main(String[] args) throws Exception {

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  int num = 1000;
  JProgressBar pb = new JProgressBar(0,num);
  pb.setStringPainted(true);
  frame.getContentPane().add(pb, BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true);

  for( int i =0; i<=num; i++){
    Thread.sleep(5);
    pb.setValue(i);
  }
  System.out.println("Finished");
}
}
</sscce>

Signature

Andrew Thompson
http://www.physci.org/

jlc488 - 25 Nov 2007 12:03 GMT
> >> > >> let's say....i have a jtextfiled ....
> ..
[quoted text clipped - 36 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-setup/200711/1

ok...if i run above codes inside the main function...then it would be
fine....but..the..problem is...

i'm running this...using Jbutton....

when i click the button...this will trigger the job....

so they are like this....

private void btnStartActionPerformed(java.awt.event.ActionEvent evt)
{
    this.gogo();
}

private void gogo(){
  try{
      for (int i = 0; i < 100000; i++) {
          this.txtNo.setText(i+""); <-- swing.JTextField
          this.txtTest.setText(i+"");  <-- this one is awt.TextField
          this.progressBar.setValue(i); <-- swing.JprogressBar
          System.out.println(i);
      }
  }catch(Exception e ){}
}

the code is like above..and the class extends JFrame.....

i was using netbean 5.5.1 to desing the UI...and somehow....txtTest
field using awt..only shows the numbers running...and...other..swing
components..are only showing the last number only...i mean like you
said hiwa...if i'm running it inside the main funciton ...

it does not have anyproblem...do you have any idea what's wrong with
this???

thanks guys...
Matt Humphrey - 25 Nov 2007 12:51 GMT
>> >> > >> let's say....i have a jtextfiled ....
>> ..
[quoted text clipped - 74 lines]
>
>thanks guys...

Being threadsafe simply means that setText can be called from another
thread--your loop is still blocking the EDT.  Andrew's version works because
the main thread is not the EDT whereas "gogo" is invoked on the EDT,
blocking it until the loop ends.  For GUI updates to be visible while they
are in progress they must be activated from a different thread so that the
EDT can keep up the job of updating the screen.

Matt Humphrey http://www.iviz.com/
jlc488 - 25 Nov 2007 13:44 GMT
> >> >> > >> let's say....i have a jtextfiled ....
> >> ..
[quoted text clipped - 85 lines]
>
> - 따온 텍스트 보기 -

oh...ok...if it is that case what should i do ??

any suggestions?? matt??
Andrew Thompson - 25 Nov 2007 16:49 GMT
>> >> >> > >> let's say....i have a jtextfiled ....
>> >> ..
>[quoted text clipped - 85 lines]

Please trim text no longer immediately relevant to a reply.
(There was no need to quote 85+ lines of earlier conversation,
just to ask your question.
...
>oh...ok...if it is that case what should i do ??

You might get some pointers from *this* JProgressBar demo.
<http://java.sun.com/docs/books/tutorial/uiswing/examples/components/ProgressBarD
emoProject/src/components/ProgressBarDemo.java


>any suggestions??

Sure.
- Post SSCCEs*, rather than code snippets.
- Spell J2SE classes with correct capitalisation, to indicate
that they are J2SE classes, rather than some 3rd party
equivalent.  On this note, it is JTextField and JButton,
as opposed to 'jtextfiled' and 'Jbutton'.
- Start each sentence with a single Capital Letter.
- Always capitalise the word 'I'.
- Add a single full-stop '.' at the end of each sentence, and
2 spaces before start of the next.
- Note that one single '?' is enough to indicate a question.

* <http://www.physci.org/codes/sscce.html>
<http://homepage1.nifty.com/algafield/sscce.html>

Signature

Andrew Thompson
http://www.physci.org/

Lew - 25 Nov 2007 18:42 GMT
> * <http://www.physci.org/codes/sscce.html>

Still shows litter:
> <%@ include file = "/codes/all_inc/pageInfo.f.html" %>
in the upper right-hand corner, and
> <%@ include file = "/all_inc/call_google_js.f.html" %>
in the upper-left, under the "Find" button.

> <http://homepage1.nifty.com/algafield/sscce.html>

Clean and vivid.  Is that yours, or a mirror?

Signature

Lew

Andrew Thompson - 26 Nov 2007 01:58 GMT
>> * <http://www.physci.org/codes/sscce.html>
>
>Still shows litter:

Will probably have a better version to upload within 24-48 hours.

>> <http://homepage1.nifty.com/algafield/sscce.html>
>
>Clean and vivid.  Is that yours, or a mirror?

That is hiwa's mirror (for which I am very grateful, given
it has been significantly more accessible than my own
copy).

Signature

Andrew Thompson
http://www.physci.org/

Matt Humphrey - 25 Nov 2007 20:05 GMT
> > Being threadsafe simply means that setText can be called from another
> > thread--your loop is still blocking the EDT. Andrew's version works
[quoted text clipped - 13 lines]
>
> any suggestions?? matt??

You have to perform the action in a separate thread, like in the following
example.

private void gogo(){
 Thread t = new Thread (new Runnable () {
    public void run () {
       try{
          for (int i = 0; i < 100000; i++) {
              this.txtNo.setText(i+"");
          }
      }catch(Exception e ){ e.printStackTrace (); }
 });
 t.start ();
}

This works only for threadsafe methods like setText.  Virtually everything
else (except repaint) is not threadsafe and you must perform the actual
update in the EDT.  (Confusing, yes?  Time-consuming task code must NOT be
in EDT, update code MUST be in EDT). The example provided by Andrew shows
how to use SwingWorker to setup a thread to do the working off the EDT and
it manages to do the done method in the EDT.  Also, that example shows the
correct way to launch a GUI in the EDT rather than in the main thread.

http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html

Matt Humphrey http://www.iviz.com/
jlc488 - 30 Nov 2007 01:41 GMT
> > > Being threadsafe simply means that setText can be called from another
> > > thread--your loop is still blocking the EDT. Andrew's version works
[quoted text clipped - 43 lines]
>
> - 따온 텍스트 보기 -

thanks..it really helped me a lot...

thank  you matt..i really appreciated...bye~~
RedGrittyBrick - 26 Nov 2007 14:42 GMT
>> Being threadsafe simply means that setText can be called from another
>> thread--your loop is still blocking the EDT.  Andrew's version works because
[quoted text clipped - 6 lines]
>
> any suggestions??

After I thought I understood threading and the dependence of most of
Swing on the Event Dispatch Thread, I found it helpful to read about
Thread.start(), SwingUtilities.invokeLater() and SwingWorker.

Something that I am currently finding useful is the code at
http://www.clientjava.com/blog/2004/08/20/1093059428000.html

After including that in my project I just insert
RepaintManager.setCurrentManager(new ThreadCheckingRepaintManager());
at the beginning of my program. It seems quite good at telling me when
I've made a mistake in running Swing code from the "wrong" thread.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.