I'm not sure what your GUI looks like, but I would use javax.swing.JTextField instead of JTextArea. If you want to use JTextArea (for example, to allow multiline messages), and you cannot make part of the input to send work, I would resort to using KeyListener as an input key, like a normal computer (if all else fails).
Here is what I mean:
import java.awt.event.*; import javax.swing.JTextArea; //or JTextField public class KeyInput implements KeyListener{ private JTextArea ta; //or JTextField public KeyInput(JTextArea ta){ //or JTextField this.ta = ta; } public void keyPressed(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_ENTER){ //code to send message goes here }else{ ta.append("\n"+e.getKeyChar()); } } //keyReleased(KeyEvent) and keyTyped(KeyEvent) methods go here, need no content }
Remember that if you use JTextArea, be sure to place it in JScrollPane.
IMPORTANT : in your client class, be sure to add ta.setEditable(false) , in which ta is the variable name of your JTextArea (again, you can replace JTextField, in which case you do not need JScrollPane).
Hope this helps.
source share