Reputation: 113
I'm a student who is trying to learn more about Java development and is currently creating a simple client/server IM application using swing.
Let me try to explain the issue I've got:
The class Login runs at start-up, and after a successful authentication with the server (which works fine) the class ClientChat gets instantiated.
ClientChat creates a new JFrame, a new connection is made with the server and new I/O streams are created. All this background stuff works fine, but the new frame is completely blank.
If I run the application without the Login class (ie by running ClientChat directly), this issue is not present.
Unsuccessful approaches so far:
From Login():
panel.add(userLabel);
panel.add(passLabel);
panel.add(userEntry);
panel.add(passEntry);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Chat Room Authentication");
frame.setSize(300, 120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
...
frame.setVisible(false);
ClientChat clientChat = new ClientChat(serverIP);
clientChat.start();
From ClientChat():
userText = new JTextField();
chatWindow = new JTextArea();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Logged in as "+user.toUpperCase());
frame.add(userText, BorderLayout.SOUTH);
frame.add(new JScrollPane(chatWindow), BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
Upvotes: 1
Views: 199
Reputation: 285405
You've got a classic Swing threading issue where you're running long-running code on the Swing's event thread also known as the Event Dispatch Thread or EDT, and by doing this, you're preventing the EDT from doing its necessary tasks of drawing the GUI and interacting with the user.
The solution: use a SwingWorker to help you create a background thread that will allow long process to not interfere with the Swing GUI and also to allow it to communicate well with the GUI.
For more on this, please check out: Lesson: Concurrency in Swing.
Upvotes: 3