Ok so i'm writing a chat program. My problem is that let's say i write something in the client or server, you only see it once you write something again. How do i make my program refresh it's self? (so it prints things in real time.) Thank You

Client Code:

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;


public class TextClient extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";


static String host=null;
String ServerInput = null;
static String portnum=null;
String ClientInput=null;
static BufferedReader In=null;
static PrintWriter Out=null;

public TextClient() {
super(new GridBagLayout());

textField = new JTextField(20);
textField.addActionListener(this);

textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);

//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}

public void actionPerformed(ActionEvent evt) {
try {
IO();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void IO() throws IOException{
String text = textField.getText();
textArea.append("Client: "+text + newline);
textField.selectAll();
textArea.setCaretPosition(textArea.g…
Out.println(text);
ServerInput=In.readLine();
textArea.append("Server: "+ServerInput + newline);
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Chat Program Client");
frame.setDefaultCloseOperation(JFram…

//Add contents to the window.
frame.add(new TextClient());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) throws UnknownHostException, IOException {

host=JOptionPane.showInputDialog("Wh… is the Ip Address to connect to?");

portnum=JOptionPane.showInputDialog(… is the port to connect to?");
int port=Integer.parseInt(portnum);

Socket skt = new Socket(host,port);

In = new BufferedReader(new InputStreamReader(skt.getInputStream()))…
Out = new PrintWriter(skt.getOutputStream(),true);

createAndShowGUI();
}
}

Server Code:

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;


public class TextServer extends JPanel implements ActionListener {
	protected JTextField textField;
	protected JTextArea textArea;
	private final static String newline = "\n";


	String ClientInput = null;
	static String Portnum=null;
	String ServerInput=null;
	static String host=null;
	static String portnum=null;
	static BufferedReader In=null;
	static PrintWriter Out=null;

	public TextServer() {
		super(new GridBagLayout());	

		textField = new JTextField(20);
		textField.addActionListener(this);

		textArea = new JTextArea(5, 20);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);

		//Add Components to this panel.
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;
		add(textField, c);

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scrollPane, c);
	}

	public void actionPerformed(ActionEvent evt){
		try {
			IO();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	public void IO() throws IOException{
		String text = textField.getText();			
		textArea.append("Server: "+text + newline);
		textField.selectAll();			
		textArea.setCaretPosition(textArea.getDocument().getLength());
		Out.println(text);
		ClientInput=In.readLine();
		textArea.append("Client: "+ClientInput + newline);	
	}

	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("Chat Program Server");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Add contents to the window.
		frame.add(new TextServer());

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) throws UnknownHostException, IOException {

		InetAddress i = InetAddress.getLocalHost();

		JOptionPane.showMessageDialog(null,"Your Ip Address is: "+i.getHostAddress()+
		"\n Give this to the Client.");
		Portnum=JOptionPane.showInputDialog("What port would you like to use?(4 Digits. Ex:4568)\n" +
		"Tell this to the client.");
		int portnumber=Integer.parseInt(Portnum);	

		ServerSocket serverSocket = new ServerSocket(portnumber);
		Socket skt =serverSocket.accept();		

		In=new BufferedReader(new InputStreamReader(skt.getInputStream()));
		Out=new PrintWriter(skt.getOutputStream(),true);

		createAndShowGUI();
	}
}

YOUR CODE DOES NOT COMPILE!!! Waste of time posting code with syntax errors!!!

Have you tried debugging your code by add println() statements at different places to show when and where the code is executing and how variables values change?

Also it'd be easier to test your code if you run both client and server in the same JVM so the debug output goes to a single console. Create a special test class and have its main() start the server and client.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.