Erik Balen
Erik Balen

Reputation: 275

How to print text to a text area

I have a text area where I want messages to be displayed in my game and I'm wondering how I would go about making a method that would print a text to the text area. Here is my GUI class:

package com.erikbalen.rpg;
import com.erikbalen.core.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Gui extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = -384241835772507459L;
private JLabel playerInfo;
private JTextField textField;
private final static String newline = "\n";
private JTextArea textArea;
private JScrollPane scrollPane;

public Gui(Player currentPlayer) {
    super("Erik's RPG");
    setLayout(new FlowLayout());        
    playerInfo = new JLabel(
       "<html>Health = " + currentPlayer.getHealth() 
               + " | " + "Mana = " + currentPlayer.getMana() + "</html>");  
    playerInfo.setBorder(BorderFactory.createTitledBorder(
               currentPlayer.getName()));
    textField = new JTextField(20);
    textField.addActionListener(this);
    textArea = new JTextArea(5, 20);
    scrollPane = new JScrollPane(textArea); 
    textArea.setEditable(false);

    add(playerInfo);
    add(textArea);
    add(textField);
    add(scrollPane);        
}

public void actionPerformed(ActionEvent textBox) {
        String text = textField.getText();
        textArea.append(text + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
        textField.selectAll();          
}   
}

So basically I want to make a method that's like:

public void printTextField(String text) {
    //print text to Gui.textArea
}

Upvotes: 3

Views: 71351

Answers (2)

EF5
EF5

Reputation: 21

Ok, you can create an OutputStream for that. The jTextArea will then be printing out in your GUI any System.out.print() or errors...

To do that you add it to where you are creating the gui components.

And you add below that for example:

PrintStream outStream = new PrintStream( new TextAreaOutputStream(jTextArea1));
        jTextArea1.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));

        System.setOut( outStream );
        System.setErr( outStream );

Then you need an inner class within your code that extends OutputStream, (here i'm still doing it for a jTextArea i initialised called 'jTextArea1')

public class TextAreaOutputStream extends OutputStream {
        private javax.swing.JTextArea jTextArea1;

        /**
         * Creates a new instance of TextAreaOutputStream which writes
         * to the specified instance of javax.swing.JTextArea control.
         *
         * @param textArea   A reference to the javax.swing.JTextArea
         *                  control to which the output must be redirected to.
         */
        public TextAreaOutputStream( JTextArea textArea ) {
            this.jTextArea1 = textArea;
        }

        public void write( int b ) throws IOException {
            jTextArea1.append( String.valueOf( ( char )b ) );
            jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        }

        public void write(char[] cbuf, int off, int len) throws IOException {
            jTextArea1.append(new String(cbuf, off, len));
            jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        }
    }

With that, all System.out's are redirected there, and it'll keep hold of all the data you give it, so you don't need that last method.

And jTextArea1 is just jTextArea for you...

Upvotes: 1

Babak Naffas
Babak Naffas

Reputation: 12571

You mean other than

public void printTextField(String text) {
    textArea.setText(text);
}

?

Upvotes: 6

Related Questions