I am writing a simple encrypted instant messenger, which involves two projects: A server and client. I got these two working very well, but decided to add a prompt on the client side to ask for a server IP Address and port number. Upon the user pressing a button, I have added an action listener for it to create an instance of the instant messenger class, which extends JFrame.
When this is done from inside this action listener (or outside in a separate method, I have tried) it creates the client JFrame, but none of the components exist inside. The client connects to the server as normal.
When the client is created in the main method, however, the program works as normal.
Here, below, is the difference between what works and what will not work:
This does not work:
okay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
port = Integer.parseInt(portField.getText());
}catch(NumberFormatException e){
return;
}
serverIP = serverIPField.getText();
dispose();
Client c = new Client(serverIP, port);
c.startRunning();
}
});
However, this does work (same class):
public static void main(String args[]){
Client c = new Client(serverIP, port);
c.startRunning();
}
All I have done, is change where the Client class is created, from inside a class constructor (called Prompt, which creates a JFrame to ask for the IP and port) to the main method. I have tried creating an instance of the Client from a new class, with the main method in, which seems to work fine.
As I stated before, the JFrame is created and is opening fine, it even connectes successfully to the server, but none of the components exist (just a blank white area). This is strange as this is not the case when it is created inside the main method.
I have little idea as to why this is happening, so if somebody could explain why, that would be brilliant.
Thanks.
Edit: I appreciate that I may have not added enough information. Here is the Client constructor:
public Client(String host, int port){
super("Instant Messenger - Client");
this.port = port;
serverIP = host;
initComponents();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
setVisible(true);
keys = new EncryptionKeys(1024);
}
Start running method:
public void startRunning(){
try{
connectToServer();
setupStreams();
exchangeKeys();
whileChatting();
}catch(EOFException eofEx){
showMessage("\nClient Terminated connection");
}catch(IOException ioEx){
showMessage("\nCould not connect to server.");
}finally{
closeDown();
}
}
And the while chatting method that is called by startRunning():
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
if(!keysSent){
if(message.substring(0, 1).equals("n")){
try{
keys.nForeign = new BigInteger(message.substring(1, message.length()));
}catch(NumberFormatException nfEx){
showMessage("Error sending keys");
}
}
else if(message.substring(0, 1).equals("e")){
try{
keys.eForeign = new BigInteger(message.substring(1, message.length()));
keysSent = true;
}catch(NumberFormatException nfEx){
showMessage("Error sending keys");
}
}
continue;
}
showEncryptedMessage(message);
}catch(ClassNotFoundException cnfEx){
showMessage("\nUser Sending error");
}
}while(!message.equals("SERVER - END"));
}
The dispose() closes the JFrame for the prompt to the user. The class Prompt is solely used to get information from the user regarding IP address and port, which is then closed, so that the Client can be opened.