Eric
Eric

Reputation: 37

textArea.setText("") doesn't clear the text in a JTextArea

I am trying to clear the text in a JTextArea, and looking at other questions, it seems like calling textArea.setText(""/null) will clear the text area. This does not seem to be happening with my code, and it appends the new text to the text already in the area. Can anyone see something wrong in my code?

public class morseJFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
public JTextPane textPane = new JTextPane();
public JTextArea textArea = new JTextArea();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                morseJFrame frame = new morseJFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public morseJFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 508);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textPane.setBounds(5, 5, 424, 194);
    textPane.setText("Enter your alphanumberic text here to translate.");
    contentPane.add(textPane);

    JButton btnTranslate = new JButton("Translate");
    btnTranslate.setBounds(5, 419, 213, 41);
    btnTranslate.addActionListener(this);
    add(btnTranslate);
    contentPane.add(btnTranslate);

    textArea.setBounds(5, 210, 424, 203);
    contentPane.add(textArea);

    JButton btnPlaySound = new JButton("Play Morse Sound");
    btnPlaySound.setBounds(228, 419, 201, 41);
    contentPane.add(btnPlaySound);
}

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("Translate")) {
        String text = textPane.getText();
        String translatedText = MorseTranslate.doMorse(text);
        textArea.setText("");
        textArea.setText(translatedText);
    }
}

}

Upvotes: 0

Views: 12181

Answers (6)

Beircheart
Beircheart

Reputation: 11

Have you inserted a simple print statement to see what textPane.getText() is actually setting String text to before sending it to doMorse(String)?

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

This does not seem to be happening with my code, and it appends the new text to the text already in the area

So based on this code...

String text = textPane.getText();
String translatedText = MorseTranslate.doMorse(text);
textArea.setText("");
textArea.setText(translatedText);

I would suggest that the problem is with your MorseTranslate.doMorse which is probably returning the text appended to itself

But, as you can see, this is a matter of "guess work" as we don't have the complete code to go by.

Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses

Upvotes: 5

user207421
user207421

Reputation: 310893

setText("") doesn't clear the text

Yes it does.

textArea.setText("");

Here you are clearing the text area.

    textArea.setText(translatedText);

Here, in the very next line, you are setting it to something else.

Upvotes: 1

NicoPaez
NicoPaez

Reputation: 436

I think setText() replaces the content (not append), so you don't need to do setText("") and then setText("the text you want"), the latest sentence should be enough.

Upvotes: 1

Abdelhak
Abdelhak

Reputation: 8387

Try to reverse the order like this:

   textArea.setText(translatedText);
   textArea.setText("");

Use either textArea.setText(null) or textArea.setText("") are the same thing.

Upvotes: 2

codehitman
codehitman

Reputation: 1188

You could alternatively try:

textArea.setText(null);

See if that works. But I agree with Wyatt, you are setting other text right after clearing it.

Upvotes: 0

Related Questions