Hello,
I have been working on a server client program that will send messages. After reading some articles on updating a JTextArea, I have came up with the println method below. My question, I read a couple of articles mentioning using invokelater from either java.awt.EventQueue or SwingUtilities. I'm not sure which is correct to use in the below method. Also, I may not be implementing it correctly, if you have any suggestions on changes I would appreciate your input. My confussion comes from looking at examples on sun where they don't even bother to us a thread to update a JTextArea, but apparently its not safe to do so without using a thread.
thanks in advance.
NOTES: this println method will be accessed from other threads to track logins and disconnects.
package com.pbuddie; import java.awt.TextArea; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import java.awt.*; public class ServerGUI extends JFrame { private static final long serialVersionUID = 1L; private float version = 0.01f; private TextArea serverLog; public static ServerGUI serverGUI; public static void main( String[] args) { serverGUI = new ServerGUI(); serverGUI.setPreferredSize(new Dimension(400, 400)); serverGUI.setTitle("Server Window " + serverGUI.version); serverGUI.serverLog = new TextArea(); serverGUI.serverLog.setEditable(false); serverGUI.add(serverGUI.serverLog); serverGUI.pack(); serverGUI.setVisible(true); serverGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); serverGUI.println("Server starting."); serverGUI.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("Closing server window."); System.exit(0); } }); } // Other threads will be accessing this to display server updates public synchronized void println(final String text) { EventQueue.invokeLater(new Runnable() { public void run() { serverLog.append(text + "\n"); serverLog.setCaretPosition(serverLog.getText().length()); } }); } }