In my GUI method I have two textFields using the Java JFrame classes. One textField sends text to the server (this function work as needed) and one textField that receives data from the server and prints it to the GUI.
I am using the textField.setText(ServerString)
method to set my text to the textField, but the .setText
method only will get the text from the string once when the GUI is started up by a command on the server and then never set the text on the textField again.
I have verified that the variable ServerString
is being live updated by the server every time something is typed but it still will not set the text to the textField. Once when it is launched, I have verified the method is being called and that .setText()
is not working after first launch. I have tried everything from try-catch
statements to setting the text to blank (which didn't work).
Is there something I am missing or some function that I am not calling or a different one I should be using?
Just an example of code so you know what libraries and stuff I am using
Here is my GUI Code that can be used to re-create the problem
package Encrypter.src;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.dv8tion.jda.events.message.MessageReceivedEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JTextPane;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
public class TalkGUI extends JFrame {
JTextPane textPane = new JTextPane();
private JPanel contentPane;
private JTextField textField;
private JTextField textField2;
//JTextPane textPane = new JTextPane();
/**
* Launch the application.
*/
public static void GUI() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TalkGUI frame = new TalkGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TalkGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 647, 448);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
System.out.println("ENTER pressed");
if(textField.getText().length() > 0)
{
}
//textField.setText("test");
//System.out.println(textField.getText());
}
}
});
textField.setBounds(10, 77, 292, 321);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblHd = new JLabel("Input to Server");
lblHd.setBounds(86, 34, 108, 32);
contentPane.add(lblHd);
JLabel lblOutputFromServer = new JLabel("Output From Server");
lblOutputFromServer.setBounds(388, 43, 174, 14);
contentPane.add(lblOutputFromServer);
textField2 = new JTextField();
textField2.setBounds(328, 77, 293, 321);
contentPane.add(textField2);
textField2.setColumns(10);
/*
textPane.setBounds(312, 77, 309, 321);
textPane.setEditable(false);
textPane.setText("you cant edit this paneen");
contentPane.add(textPane);
*/
}
public void set(String string)
{
System.out.println(string + " ok");
textField2.setText(string);
}
}