I have created an application in which a new JFrame
displays certain output.
The application is working fine when I call the method new ServerInitiator().Initialize(8081)
from a console application.
But when I call the method from a button click event in first frame the second frame is blank and does not display any output.
Code on first JFrame
is
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new ServerInitiator().initialize(8081);
}
Code for ServerInitiator
is
public class ServerInitiator {
private JFrame frame = new JFrame();
private JDesktopPane desktop = new JDesktopPane();
public static void main(String args[]){
}
public void initialize(int port){
try {
ServerSocket sc = new ServerSocket(port);
//Show Server GUI
drawGUI();
//Listen to server port and accept clients connections
while(true){
Socket client = sc.accept();
System.out.println("New client Connected to the server");
//Per each client create a ClientHandler
new ClientHandler(client,desktop);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/*
* Draws the main server GUI
*/
public void drawGUI(){
frame.add(desktop,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Show the frame in a maximized state
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}