Results 1 to 7 of 7
  1. #1
    sathya11491 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Capturing System.out.println() and directing it into JtextArea

    hello guys

    Consider this sample code

    Java Code:
    public int[] sample()
    {
    int a[];
    int x=1;
    for(int i=0;i<10;i++)
    {
    a[i]=x++;
    System.out.println(a[i]);
    }
    return a;
    }
    now i have a jTextArea. I want to print the contents of array 'a' in the jtextArea

    consider this code for the above.......

    Java Code:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    int b[]=sample();
    for(int y:b)
    jTextArea.append(y)
    }
    now every thing works fine and good. but the problem is that the array in use here has only 10 elements. but actually, in the context of my application, it ranges upto 1000. so after the action event is triggered,my application has to wait untill the sample() returns all the 1000 elements(after processing them) before displaying them in jTextArea. This is really irritating...

    What i want instead is that i want my jTextArea to display the array items one after the next as it is processed -i.e. i want a functionality like redirecting my System.out.println(a[i]) directly to my jTextArea, so that the jTextArea displays the array items one after the other as they are processed instantaneously as System.out.println(a[i]) prints in console.

    Note that both the methods are in different classes and different packages.

    looking forward for your help guys,
    regards,
    sathya
    Last edited by Eranga; 03-07-2012 at 05:16 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Capturing System.out.println() and directing it into JtextArea

    First of all, please use code tags when you are posting code segments in the forum. Unformatted codes are really hard to read.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Capturing System.out.println() and directing it into JtextArea

    Your question is, you have an array of 1000 elements and you want to display them in a text area. Are these event take place asynchronously or not? And what you are doing with the array element. In general to fill up an array, unless your processing doing some rubbish, cannot take a long time. So you could wait until fill the array to display them.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Capturing System.out.println() and directing it into JtextArea

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    sathya11491 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Capturing System.out.println() and directing it into JtextArea

    Eranga, next time ill sure use code tags..

    Iam having 1000 ips in an array. Iam pinging each one and if it is reachable, iam moving it to another array.so it does (pinging) takes quite some time (1.5 s for each ip).

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Capturing System.out.println() and directing it into JtextArea

    Change 'sample()' so it takes a parameter.
    That parameter will be an interface.
    It will have a single method (at the moment) 'append(int)', or 'println(int)' possibly.
    Pass an object that implements that method into the call to sample().
    The implementation of that method, in the case of your button action, will simply append to the text area.
    This allows you to pass any implementation into sample() to do things other than append to a text area...eg using the System.out.println() call.

    ETA: Oh yes, and this should all be done in a SwingWorker thread, so your GUI doesn't lock up.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Capturing System.out.println() and directing it into JtextArea

    Even on the same design if you have an idea about threads, you have so many options to take.

Similar Threads

  1. Replies: 8
    Last Post: 06-21-2011, 03:21 PM
  2. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  3. System.out.println to a jtextarea
    By masterhoulahan in forum AWT / Swing
    Replies: 7
    Last Post: 07-07-2009, 04:11 PM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM
  5. Help me with system.out.println
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •