Hello all, I'm working on a simple self-chat program, which will eventually lead into a server/client chat program, but for now I am trying to learn the basics. When I run my applet, when I type something in the textarea, it shows up in the text field just fine, but when I backspace whatever is in the text area, it adds letters to what is displayed in the text field.

For example:

I type hello in the line provided;
now if i backspace i'll get this in the text field hellolleh.

this is the code I have: I'm wondering what I need to add so that the textarea will delete the characters, and not add them when I press backspace. Something with Keycode I'm assuming, just unaware of how to set this up.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class AppletThree extends Applet implements ActionListener, TextListener {

    TextField tf;
    TextArea ta;

    Button b;



    public void init() {


        setLayout(new BorderLayout());

        tf = new TextField();
        add(tf, BorderLayout.NORTH);

        ta = new TextArea();
        add(ta, BorderLayout.CENTER);

       //tf.addActionListener(this);

        tf.addTextListener(this);

        b = new Button("Clear");
        add(b, BorderLayout.SOUTH);

        b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == b) {
            ta.setText("");
        } else {
            String temp = tf.getText();

            ta.append(temp + "\n");


            tf.setText("");

        }
    }
    public void textValueChanged(TextEvent te) {

        String temp = tf.getText();

        char c = temp.charAt(temp.length()-1);

        ta.append(""+c);



    }
}

What was the idea behind your textValueChanged? When the text is changed your code then duplicates the last letter - that seems wrong.

probalby

1) you want to use

   a) ActionListener for TextField only, then remove TextListener from TextField (standard logics for Chat)

   b) both, then content inside public void textValueChanged(TextEvent te) { must to know if is there event fired from ActionListener or KeyTyped, e.g. by using boolean for creating String or Char

2) note use JApplet, JTextField, JTextArea, DocumentListener instead of TextListener, because you wrote an program on resoures valid in last milenium

Edited 3 Years Ago by mKorbel

@jamescherrill, it was a practice problem I had worked on about a week ago in class, I'm trying to extend the program so I can practice and get better with Java (big novice). I'm figuring I need to use KeyListener, and KeyEvent or something, but I'm not exactly sure where I need to implement it. I know ill need something along the lines of public void KeyPressed(KeyEvent ke) etc: something like that, just not entirely sure how to get it to recognize that when I press backspace in the textfield, the textarea also gets backspaced.

Edited 3 Years Ago by minimee120

I'm figuring I need to use KeyListener, and KeyEvent or something, but I'm not exactly sure where I need to implement it.

not don't to use KeyListener in AWT/Swing, nor for (J)TextComponents

You get that behaviour because you copy the last character in one field to the other - that's the last char of the text after Java has updated it from the keyboard, it's not the last key that was entered. So with "world" in the field, after a backspace the field contains "worl", then you copy the "l" from that.
Simplest solution is propbably just to copy the entire text each time, not to try to do incremental updating.

This article has been dead for over six months. Start a new discussion instead.