Click here to Skip to main content
15,892,161 members

Comments by nameJulian (Top 14 by date)

nameJulian 13-Jun-13 3:20am View    
" The PrintStream class enables you to write formatted data to an underlying OutputStream. For instance, writing int, long and other primitive data formatted as text, rather than as their byte values. "
from: http://tutorials.jenkov.com/java-io/printstream.html.
nameJulian 30-May-13 6:54am View    
and the continuity:

public static void main(String[] args) {

//ChatApplication program=new ChatApplication();
//program.createInterface();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName);
window.setTitle(userName);

}catch(IOException ex){
JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
}
}

});
}


public void actionPerformed(ActionEvent e) {

if(e.getSource()==sendButton){
sendFlag=true;

try{
String messageToServer=new String("");
messageToServer=messageField.getText();

output.println(messageToServer);
messageField.setText("");

}catch(NullPointerException ex){
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}



/*
* clasa se ocupa cu crearea unui thread pentru a putea citi de la server
*/

class ServerMessageThread extends Thread{


public void run(){

String serverResponse=new String();

try {
while(true){
serverResponse=input.readLine();
messagesTextArea.append(serverResponse+"\n");
}
} catch (IOException e) {
messagesTextArea.setForeground(Color.RED);
messagesTextArea.setText("Eroare citire de la server: "+e.getMessage());
}
}
}

/*
* clasa care se ocupa cu crearea firelor de executie a clientilor
*/

}
nameJulian 30-May-13 6:47am View    
this is the client class:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;


public class ChatApplication implements ActionListener{

static JFrame window;
static Socket clientSocket;
Socket serverClientSocket;
BufferedReader input;
PrintWriter output;
static JTextArea messagesTextArea;
static JTextArea usersTextArea;
static JTextField messageField;
JButton sendButton;
JButton connectButton;
JButton startServerButton;
String userName=new String();
static boolean sendFlag=false;


public ChatApplication(){}

public ChatApplication(String userName,String serverName) throws UnknownHostException, IOException{
this.userName=userName;
clientSocket=new Socket(serverName,9999);
input=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
output=new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()),true);
output.println(userName);
createInterface();
new ServerMessageThread().start();
}

public void createInterface(){
window=new JFrame();
window.setSize(600,600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BoxLayout(window.getContentPane(),BoxLayout.Y_AXIS));

sendButton=new JButton("Send");
connectButton=new JButton("Connect");
startServerButton=new JButton("Start");

messageField=new JTextField(10);
messagesTextArea=new JTextArea(40,40);
messagesTextArea.setEditable(false);
usersTextArea=new JTextArea(40,10);
usersTextArea.setEditable(false);

JScrollPane scrollPane=new JScrollPane(messagesTextArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

JPanel initialPanel=new JPanel();
initialPanel.setLayout(new BoxLayout(initialPanel,BoxLayout.X_AXIS));
initialPanel.add(connectButton);
initialPanel.add(startServerButton);

JPanel upPanel=new JPanel();
upPanel.setLayout(new BoxLayout(upPanel,BoxLayout.X_AXIS));
upPanel.setBorder(new TitledBorder(""));
upPanel.add(scrollPane);
upPanel.add(usersTextArea);

JPanel bottomPanel=new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel,BoxLayout.X_AXIS));
bottomPanel.setBorder(new TitledBorder(""));
bottomPanel.add(messageField);
bottomPanel.add(sendButton);

window.add(initialPanel);
window.add(upPanel);
window.add(bottomPanel);

sendButton.addActionListener(this);

// connectButton.addActionListener(new ActionListener(){
//
// public void actionPerformed(ActionEvent e){
//
// String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
// try{
// String serverName="localhost";
// new ChatApplication(userName, serverName);
// window.setTitle(userName);
//
// }catch(IOException ex){
// JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
// }
//
// }
//
// });

startServerButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
Thread thread=new Thread(){
public void run(){

ChatServer server=new ChatServer();
try {
server.process();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
};

thread.start();


}

});

}

public static void main(String[] args) {

//ChatApplication
nameJulian 30-May-13 3:28am View    
I had given up up the default constructor and called the parameterized constructor directrly intro the main method( in the constroctor calling the createInterface method), like this:
public static void main(String...args){
try{

String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName); //call to appropriate constructor
window.setTitle(userName);
}catch(IOException e){
System.err.println(e.toString());
}

}//main
But i'm still geting the same exception.
nameJulian 29-May-13 13:22pm View    
The exception occurs in the actionPerformed method of sendButton, on the line: output.println(messageToServer);