Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Text output in GUI

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Text output in GUI

    Hi guys.

    I'm new to this forum, and have been programming java "actively" for a couple months or so now.

    Basically, I'm currently writing a program with a GUI, which will among other things have a text-out put panel. I'm wondering, in general, if you want to output text to a gui (no input required in this case), in what way would you guys prefer to implement it?

    First off I am right now using a JTextPane, but this might change. I've also tried to simply set the text with the setText() method, which works ok for the first line but doesn't appear to update when I use it a second time, and since the class has no redraw() method I'm a little at a loss.

    Personally, I would LIKE to setup some kind of outputstream which is linked to the JTextPane component, but I'm finding it hard to find any good explanations on how to do this. I've got the impression I need to create an outputstream that writes to the Document of the JTextPane. Can anyone explain how I would do this, since this my preferred solution?

    Thanks for your help.

    // Andreas


  2. #2
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Text output in GUI

    Hello aspiring programming,this is a good example of the power of StringBuilder

        public StringBuilder consoleText = new StringBuilder();
     
        public void println(String s) {
            consoleText.append(s + "\n");
            console.setText(consoleText.toString());
        }

    "console" being the name of your JTextPane.

    I hope you learned from this :p

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Text output in GUI

    if you want to output text to a gui (no input required in this case), in what way would you guys prefer to implement it?
    An appropriate subclass of JTextComponent with setEditable(false).

    setText() method, which works ok for the first line but doesn't appear to update when I use it a second time
    Impossible to guess at the reason. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Location
    Nigeria, Lagos
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Text output in GUI

    If you need to output the previous messages you can as well try this
    public StringBuilder consoleText = new StringBuilder();
        public void println(String s){
         consoleText.append(s + "\n");
         console.setText(console.getText() + "\n" + consoleText.toString());
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Text output in GUI

    adeloyedeji, welcome to the forums. Please be mindful of post dates, you resurrected a 3 years old post, and not only that you posted virtually identical code already given

Similar Threads

  1. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  4. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM