Reputation: 1376
I have an object ReminderGUI
which has a JTextArea
field. ReminderGUI
represents an app which lets save and display reminders. When getReminderButton
is clicked I want the app to find the reminder which was previously saved for this date and display it in the JTextArea
(I'm not showing this functionality in the code snippet).
I'm having trouble with changing JTextArea
text and the code below demonstrates it. Once getReminderButton
is clicked then getReminderButtonHandler()
is supposed to initialize a new blank JTextArea
and then append it to some new text here
. Why doesn't this work?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ReminderGUI extends JFrame implements ActionListener{
private JButton getReminderButton;
private JTextArea reminderTextArea;
public ReminderGUI() {
super();
super.setLayout(new BorderLayout());
this.reminderTextArea = new JTextArea("Enter text");
this.getReminderButton = new JButton("Get reminder");
JPanel southPanel = new JPanel();
southPanel.add(getReminderButton, BorderLayout.SOUTH);
super.add(southPanel, BorderLayout.SOUTH);
super.add(reminderTextArea, BorderLayout.CENTER);
this.getReminderButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.getReminderButton) {
this.getReminderButtonHandler();
}
}
private void getReminderButtonHandler() {
this.reminderTextArea = new JTextArea("");
this.reminderTextArea.append("some new text here");
}
public static void main(String[] args) {
ReminderGUI rmg = new ReminderGUI();
rmg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rmg.setSize(500, 300);
rmg.setVisible(true);
}
}
Upvotes: 0
Views: 413
Reputation: 2452
The problem is in this line: this.reminderTextArea = new JTextArea("Enter text");
you're creating a new TextArea
You can set it using the set
method, like this: reminderTextArea.setText(text);
Upvotes: 1