简体   繁体   中英

How do I add text to a JTextArea?

I'm making a java Text Editior and I can't seems to know how to insert a line of text that is "[code][/code]" Here is what I'm trying to program. The method for the inserting is called "insert". So it has to be something that is insert,(something that inserts strings of text in JTextArea )

/////////////////// CODE //////////////////////////////////////////////////////////////////////////////////////////////////

this.insert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


        }
    });
/////////////// END OF CODE ///////////////////////////////////////////////////////////////////

Simple example to set/assign text to JTextArea .. this is not the solution but it will help you...

JTextArea textArea = new JTextArea(
    "This is an editable JTextArea. " +
    "A text area is a \"plain\" text component, " +
    "which means that although it can display text " +
    "in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

Although to set the text .. use this method

void insert(String str, int pos) 
Inserts the specified text at the specified position.

public void setText(String t)
Sets the text of JTextArea

For refrerence and help please follow jtextareaguide

Link to video tutorial

Guide for Simple Editor in Java

use:

JTextArea text=new JTextArea();
  text.setText("Message..");

Here is a doc.

public class JTextArea extends JTextComponent

A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. You can find information and examples of using all the text components in Using Text Components, a section in The Java Tutorial.

Try this:

JTextArea textj1 = new JTextArea();
textj1.setText(textj1.getText().trim() + "a string or even an arraylist");

Java already provides a method for inserting text in the JTextArea class. Try this...

 JTextArea t = new JTextArea();
 t.setText("specified string");
 t.append("+ added string");

更好地使用:

textArea.setText(textArea.getText()+" Message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM