Results 1 to 7 of 7
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default Get all System.out.println() to redect to JLabel

    I have already made a program that is command-line, I just want to convert it to a very basic GUI using a single JLabel and JTextField (so that it will still have pretty much the same interface as command-line).

    I haven't done any work on swing yet but have just tried learning some swing using online tutorials and think i have gained some of the basic concepts.


    Can I have a JLabel whos parameters are System.out.println's output?
    i.e. instead of what would otherwise print to the command line, it would print to the JLabel?



    Sorry if what I put makes no sense or if I should be doing something completely different, I am new to swing.


    TL;DR
    How can I get System.out.println() to direct to a JLabel or what command can I use instead of System.out.println() to print to a JLabel multiple times?
    Last edited by kkid; 11-24-2012 at 05:38 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Get all System.out.println() to redect to JLabel

    Can I have a JLabel whos parameters are System.out.println's output?
    i.e. instead of what would otherwise print to the command line, it would print to the JLabel?
    Yes, although a JTextArea might be better for multi-line prints. System.out is a PrintStream, you can set this to your own PrintStream instance (see the API for System class) based upon an OutputStream that directs the data to wherever you want, in this case it can direct it to your JTextArea.

  3. #3
    rougeking is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Get all System.out.println() to redect to JLabel

    Yes,u can.....
    direct the data in to setText() method of your component....
    and use validate() method to update your screen....
    thats it....
    hope its useful :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Get all System.out.println() to redect to JLabel

    Quote Originally Posted by rougeking View Post
    Yes,u can.....
    direct the data in to setText() method of your component....
    and use validate() method to update your screen....
    thats it....
    hope its useful :)
    Sorry but this answer is completely and hopelessly wrong. He's talking about *redirecting* the standard output, and doWhile explains how this can be done. setText(...) will do nothing of the kind, and validate() or revalidate() are never necessary for showing text changes in a JLabel.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default Re: Get all System.out.println() to redect to JLabel

    Quote Originally Posted by Fubarable View Post
    Sorry but this answer is completely and hopelessly wrong. He's talking about *redirecting* the standard output, and doWhile explains how this can be done. setText(...) will do nothing of the kind, and validate() or revalidate() are never necessary for showing text changes in a JLabel.
    I was getting a little confused by his response, thanks.

    So what is it I need to do? If it is actually possible?

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Get all System.out.println() to redect to JLabel

    Quote Originally Posted by kkid View Post
    So what is it I need to do? If it is actually possible?
    Did you attempt what I suggested in post #2? If so, what happened? At what step are you confused? If not, give it a try (to add to my previous post, you might need to implement OutputStream, which then appends to an internal buffer when write is called and appends to your UI component when flush is called).

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default Re: Get all System.out.println() to redect to JLabel

    Quote Originally Posted by doWhile View Post
    Did you attempt what I suggested in post #2? If so, what happened? At what step are you confused? If not, give it a try (to add to my previous post, you might need to implement OutputStream, which then appends to an internal buffer when write is called and appends to your UI component when flush is called).
    OMG, I didn't even see your post, sorry!

    I did wonder why Fubarable mention doWhile as I though he was talking about do-while loops lol :P

    Ill do your suggestions now, I'm embarrassed now

Similar Threads

  1. System.out.println
    By chandrasekhar.melam in forum New To Java
    Replies: 8
    Last Post: 09-19-2011, 07:53 PM
  2. Replies: 8
    Last Post: 06-21-2011, 02:21 PM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM
  5. System.out.println
    By Sniper-X in forum Advanced Java
    Replies: 10
    Last Post: 05-05-2008, 03:41 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
  •