-2

Good Evening, I am new to GUI in Java. I have the following code :

public class FrameENN extends JFrame {

JButton b1;
JTextArea t1;
JLabel l1;
String c;
eHandler handler = new eHandler();

public FrameENN(String s) {
    super(s);
    setLayout(new FlowLayout());
    b1 = new JButton("Get Result");
    t1 = new JTextArea(5, 25);
    l1 = new JLabel("");
    add(b1);
    add(t1);
    add(l1);
    b1.addActionListener(handler);
}

public class eHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {

            for (int i = 0; i < 3; i++) {
                **System.out.println(i);**

            }

        }
    }

}

}

What can I do when pressing the button to get this System.out.println(i) in JTextArea? I tried with t1.append but it didn't work. Thank You.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

2 Answers2

1

You have to change the standard out stream to the JTextArea. There's a really nice example here that shows you how to do this!

Basically, what you want to do is create your own custom OutputStream and override the write(int) method, which is called when bytes are being printed through the stream. You append text to the TextArea in the write method. The stream itself is created by making a new PrintStream instance, with the custom output stream you just made as a constructor parameter.

FlashDaggerX
  • 89
  • 10
  • If you're going to link to offsite resources/other answers, you should provide some supporting information about the additional link, so if the link goes off line, the answer will still be relevant – MadProgrammer Jan 03 '18 at 01:56
  • Understood, will do in answers for the future. – FlashDaggerX Jan 03 '18 at 01:58
1

append works fine.

t1.append(i+"");

will do.