Hi,
I am developing a Swing/Java app but having difficulties passing data from the initiliaser threads to one of the GUIs.
The GUI is initialised by invoking an 'initComponents()' method on the event dispatch thread in its run() method:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
ChatSession ed = new ChatSession(client);
ed.initComponents();
ed.setVisible(true);
}
});
In ChatSession.java, I have a method initComponents() which contains the line:
conversation = new javax.swing.JTextArea();
I then need to update the GUI from another method in the ChatSession.java object which is listening for TCP packets. I need to append new text that comes in over the TCP socket to the 'conversation' JTextArea on the GUI.
How can I do this? I have tried using invokeLater to run a method on the event dispatch thread, but I get a NullPointerException error...???
java.awt.EventQueue.invokeLater(
new Runnable() {
public void run()
{
//conversation.append(text);
ChatSession.this.conversation.append("text to append to chatbox");
}
});
I have also tried the commented out line and still get a NPE.
I also need to pass data from the event dispatch thread to the ChatSession object on click of a button (in order to send it over the network to another client).
I have a method in ChatSession.java which is assigned to respond to clicks of the button using an Action Listener initialised on the event dispatch thread:
The method in ChatSession.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// add to toSend buffer
toSend.append(sendtext.getText());
}
.... is assigned to an ActionListener in initComponents() , part of ChatSession.java, (which is run on the event dispatch thread, see earlier):
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
So how do I pass data from the event dispatch thread to the 'initialiser' object, and then how do I pass data from the initialiser back to the event dispatch?