currently I'm programming a Chat and I noticed whenever I write something in my JTextfield without sending it to the Server the current contents of the JTextfield resets to "". In my Code some String parts are in german but this shouldnt be a big problem I hope.
First Here is a Screenshot of the empty JTextfield: Then I write something in the JTextfield: And after that the test text "hg.." gets deleted after 2-4 seconds without pressing anything.
public class ClientWindow extends JFrame implements Runnable{
private static final long serialVersionUID = 7413464033743652908L;
private JPanel contentPane;
private JMenuBar bar;
private JMenu datei;
private JMenu option;
private JMenu features;
private JMenuItem verlaufSpeichern;
private JMenuItem w;
private JMenuItem ww;
private JTextArea history;
private JTextField text;
private JButton send;
private DefaultCaret caret;
private Client client;
private Thread run, listen;
public ClientWindow(String name, String address, int port){
client = new Client(name,address,port);
createWindow();
createPanel();
createLookAndFeel();
createJMenu();
createInhalt();
console("Versuche zu Verbinden.(Username: "+ name +", IP: "+ address +", Port: "+ port +")");
boolean connect = client.openConnection(address);
if(!connect){
console("Verbindung fehlgeschlagen.");
}
String connection = "/c/" + name;
client.send(connection.getBytes());
run = new Thread(this,"Running");
run.start();
}
public void run(){
listen();
}
public void listen(){
listen = new Thread("Listen") {
public void run(){
while(true){
String message = client.receive();
if(message.startsWith("/c/")){
client.setID(Integer.parseInt(message.split("/c/|/e/")[1]));
console("Erfolgreich verbunden.");
} else if(message.startsWith("/m/")){
System.out.println(message);
String text = message.substring(3);
text = text.split("/e/")[0];
console(text);
} else if(message.startsWith("/i/")){
String text = "/i/" + client.getID() + "/e/";
send(text, false);
}
}
}
};
listen.start();
}
public void createLookAndFeel(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public void createJMenu(){
bar = new JMenuBar();
datei = new JMenu(" Datei ");
option = new JMenu(" Options ");
features = new JMenu(" Features ");
verlaufSpeichern = new JMenuItem(" Verlauf speichern ");
w = new JMenuItem(" Coming Soon ");
ww = new JMenuItem(" Coming Soon ");
datei.add(verlaufSpeichern);
option.add(w);
features.add(ww);
bar.add(datei);
bar.add(option);
bar.add(features);
setJMenuBar(bar);
}
public void createWindow(){
setResizable(false);
setTitle("Chat - "+ client.getName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,550);
setLocationRelativeTo(null);
setVisible(true);
}
public void createPanel(){
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
public void createInhalt(){
history = new JTextArea();
JScrollPane scrollHistory = new JScrollPane(history);
scrollHistory.setBounds(10,10,775,450);
history.setEditable(false);
caret = (DefaultCaret) history.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
contentPane.add(scrollHistory);
text = new JTextField();
text.setBounds(10, 465, 700, 30);
text.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
send(text.getText(),true);
}
}
});
contentPane.add(text);
send = new JButton("Senden");
send.setBounds(715, 465, 70, 30);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
send(text.getText(),true);
}
});
contentPane.add(send);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
String disconnect = "/d/"+ client.getID()+"/e/";
send(disconnect,false);
client.close();
}
});
text.requestFocusInWindow();
}
public void send(String message, boolean textB){
if(message.equals("")) return;
if(textB){
message = client.getName() + ": " + message;
message = "/m/"+message;
}
client.send(message.getBytes());
text.setText("");
text.requestFocusInWindow();
}
public void console(String message){
history.append(message + "\n\r");
history.setCaretPosition(history.getDocument().getLength());
}
}
In the method send you have the text set to "" in this line:
text.setText("");
Here the method that I have mentioned:
public void send(String message, boolean textB){
if(message.equals("")) return;
if(textB){
message = client.getName() + ": " + message;
message = "/m/"+message;
}
client.send(message.getBytes());
text.setText("");
text.requestFocusInWindow();
}`
I guess if you remove that line the text will not be reset and it will solve your problem.