Seeker
Seeker

Reputation: 509

How to add text to JTextArea? java

I have three buttons in in my program and a JTextArea. What I want to do is, when the user presses a button/s I want the JTextArea to have the text saying button 1 was pressed, button 2 was pressed and so on. for example.

JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();

JTextArea text = new JTextArea();
JFrame frame = new JFrame();
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(text);
frame.setVisible(true);

What I want to do is, when the user presses button 1, I want the JTextArea to have the text saying Button 1 was press and then if the user presses button 2, I want the JTextArea to have the previous text and the text for button 2. so it should say something like;

button 1 was pressed
button 2 was pressed

edit:

so have the text like so,

button 1 was pressed button 2 was pressed
button 3 was pressed 

and if I had more buttons, it will look like this

button 1 was pressed button 2 was pressed
button 3 was pressed button 4 was pressed
button 5 was pressed button 6 was pressed

and so on.

Upvotes: 1

Views: 2343

Answers (3)

Pshemo
Pshemo

Reputation: 124215

Add actionListener to each botton which will invoke

yourTextArea.append("button X was pressed\n");

Here is simple demo

JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());

final JTextArea area = new JTextArea(2,20);
frame.getContentPane().add(area);

JButton button1 = new JButton("press me");
JButton button2 = new JButton("press me");

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        area.append("button 1 was pressed\n");
    }
});
button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        area.append("button 2 was pressed\n");
    }
});

frame.getContentPane().add(button1);
frame.getContentPane().add(button2);

frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

You can also use

try {
    area.getDocument().insertString(0,"button 1 was pressed\n", null);
} catch (BadLocationException e1) {
    e1.printStackTrace();
}

instead of

yourTextArea.append("button X was pressed\n");

if you want to add new lines at start of text area.

Upvotes: 2

Before you add the button to the frame do this:

button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Here write what you want to be execute when button is pressed
        }
    });   

For better details about this visit : http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

You need to add action listeners to your buttons something like this:

button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        textArea.append("button 1 was pressed");

    }
});

Dont forget to declare textArea at class level.

Hope this helps

Upvotes: 2

Related Questions