user3062664
user3062664

Reputation: 1

Java jButton and jTextArea

I am having a problem outputting certain information to my jTextArea. At the moment, When I click a button, the information is being drawn from an arrayString and output using System.out.print. I have another panel that holds the jTextArea and instead of getting the information to print on the System.out.print, I would like to get it to print on the jTextArea instead. I am sure it is simple enough, I am just not sure how to do it.

This is my ActionListener for my button:

jbPlay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            jbPlay.addActionListener(this);

            System.out.println(MyFrame.shape1.getArrayList());


        }
    });

And here is my class for the panel which contains the jTextArea

ublic ActionPanel() {
    initComponents();


    jtaWoof = new JTextArea();
    jtaWoof.setVisible(true);

    jtaWoof.setEditable(true);
    jtaWoof.setSize(900,400);
    jtaWoof.setBackground(Color.white);
    this.add(jtaWoof);}

I have also created getters and setters for the jtaWoof; not sure if I needed to or not!

public JTextArea getJtaWoof() {
    return jtaWoof;
}

public void setJtaWoof(JTextArea jtaWoof) {
    this.jtaWoof = jtaWoof;
}

Thank You in advance for any help, it is very much appreciated!

Upvotes: 0

Views: 481

Answers (2)

disrvptor
disrvptor

Reputation: 1612

Replace this line

System.out.println(MyFrame.shape1.getArrayList());

with

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        jtaWoof.setText(jtaWoof.getText()+"\n"+MFrame.shape1.getArrayList());
        // or use append()
        //jtaWoof.append(MFrame.shape1.getArrayList()+"\n");
    }
}

This will replace the text of the JTextArea with the text of the JTextArea plus a newline plus the new text. I would caution you to be careful since if the amount of text isn't limited in some way you could run into performance problems. Especially since this is run on the EDT (via the invokeLater(..) method.

Upvotes: 0

SomeRandomName
SomeRandomName

Reputation: 603

or you can just use append..

public void append(String str)

Appends the given text to the end of the document. Does nothing if the model is null or the string is null or empty.

Parameters: str - the text to insert See Also: insert(java.lang.String, int)

Example

jtaWoof.append(MyFrame.shape1.getArrayList().toString());

Upvotes: 1

Related Questions