creedjarrett
creedjarrett

Reputation: 158

Opening a new GUI from a message returned from server Java

I am designing a project for a class that opens a series of GUI's before reaching the GUI. each component functions independently but when we try and use a message returned from a server to create a new GUI, it makes the window, but with none of the components. any ideas?

for example.

we have a log in screen come up, which then sends the ID to the server, which returns a list of available whiteboards to the client, this works fine. then when we want the client to create a GUI after the message received it comes up with a box that is titled correctly but none of the components are there. i had this working earlier skipping the available boards and just opening a whiteboard, none of the components of the whiteboard were created although, the computer thought it had created a whiteboard successfully.

we tried something like this...

ChooseBoardGUI newChoose = new ChooseBoardGUI(out, availableBoards);
newChoose.setVisible(true);
newChoose.pack();
String numberOfBoard = in.readLine().substring(2);
WhiteboardGUI a = createWhiteboard(out, numberOfBoard);

any ideas on why the new GUI would come up blank?

thanks for any help

Upvotes: 0

Views: 58

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Possible problems:

  • You're calling a long-running task on the Swing event thread, tying it up so that it can't paint the GUI as it is supposed to do. Perhaps you're waiting on a socket to send information in but doing this within the EDT, or Swing Event Dispatch Thread. This is a common cause of a window being created but not being properly painted.
  • Or you've got a bug in code not shown (well this is undoubtedly true since you're not showing us any relevant code, and so I should re-phrase it, "you've got a bug in code not shown, other than described in the preceding bullet").

The solution is:

  • If your problem is due to the first issue, then be sure to have all long-running tasks run in a thread background to the Swing event thread. A SwingWorker can work well for this.
  • If your problem is the second issue, well then show us more code and give more information. Preferably create and post an sscce (please check out the link).

As an aside: it sounds as if your GUI is structured to throw several JFrames at the user. If so, consider re-designing your code so that your separate GUI's are geared towards creating JPanels, and then have your program show one main stable JFrame but swaps "views", again usually JPanels, via a CardLayout.


A great tutorial on Swing Concurrency: Lesson: Concurrency in Swing

Upvotes: 2

Related Questions