-2

I have a void method that prints some stuff. Can (swing)TextField be filled with the content of my print, somehow? Or should I use something different?

if my method prints:

Hello
how
are
you?

I would like to show on the window, exactly that. Thank you.

private class ButtonListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        label.setText(instance.voidMethodThatPrints());
    }
}

It does not work cause setText takes a string, so I was trying to ask you if there's a faster way to make it work.

Matt
  • 773
  • 2
  • 15
  • 32
  • Can you post what you have tried already? You can use a TextFields setText() method to display text. – Revive Aug 05 '14 at 10:10
  • @I-LOVE-2-REVIVE look at the code, it clearly won't work – Matt Aug 05 '14 at 10:21
  • 2
    can you change the void method to return a String? The only other thing I can think of is you could change System.out to use a custom PrintWriter as in Kayamans answer. Heres a link http://stackoverflow.com/questions/564913/how-to-print-to-textarea-instead-of-console-in-eclipse – Revive Aug 05 '14 at 10:32

2 Answers2

1

What's wrong with textArea.append(text)? Call it with appropriate arguments and get what you need.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • textArea too requires a String, I'm talking about a void mathod. Look at the code on the top. – Matt Aug 05 '14 at 10:25
  • 1
    I am sorry, @Matt I have no idea what do you want. Do you want a void method that "prints" something into textarea? But text area is not a output stream. You can (if you want) implement `PrintWriter` that in fact calls `textarea.append()`. The question remains here is "why" – AlexR Aug 05 '14 at 10:34
1

You can use System.setOut(PrintWriter pw) to redirect the standard output. However you'll have to write some additional logic to make it go to a text area (since there's no setOut(JTextArea jt) method).

Edit: This approach is not recommended for your situation. The correct approach in your case would be to redesign your code, such as suggested by I-LOVE-2-REVIVE in the comments.

Kayaman
  • 72,141
  • 5
  • 83
  • 121