setVisible appears to be jamming screen

I am currently having a problem where I open a new window in a separate class to display a game board. When the window is opened however none of the components show despite setVisible(true) being called. In this class a nework server is also being set up, which could be causing the jam. Although this class runs fine when not loaded from the previous class.

Sorry this is probably not the best explanation ever, here is the code where the new window is called in the initial class:

if (cmd.equals("create")) {

NetServer host = new NetServer();

frame1.setVisible(false);

}

NB: Create is simple an action command

Here is the code for the called class:

Constructor:public NetServer() {

setJMenuBar(menuBar);

setLayout(new BorderLayout());

addWidgets();

add(board, BorderLayout.CENTER);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setTitle("Atomz Server Player");

setSize(400,400);

SwingUtilities.invokeAndWait(this.setVisible(true));

netServ();

}

netServ() method:

public void netServ() {

String lostcon = "";

try {

ServerSocket s = new ServerSocket(3000);

while (true) {

Socket incoming = s.accept();

BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));

PrintWriter out = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));

while (true) {

if(!ClientGo){//SERVERS GO

if(ServerTrigger){

out.println(ServerMove);

out.flush();

ClientGo = true;

ServerTrigger = false;

}

}

out.flush();

if(ClientGo){//CLIENTS GO

String str = in.readLine();

System.out.println(str);

int actx, acty;

String act = new String();

for(int i=0;i<gridx;i++) {

for (int j=0;j<gridy;j++) {

actx = i;

acty = j;

act = String.valueOf(i) + String.valueOf(j);

if (str.equals(act)) {

String atoms = play[j].getLabel();

if (gplayer.equals("red")) {if(play[j].getBackground()!=Color.blue){ // check if legal move

addAtom(actx, acty, atoms, gplayer);// red player

gplayer = "blue";

sysmessage.setText("Blue Player's Turn");

out.flush();

} // end legal check

}

}

}

}

if(str == null){

lostcon = null;// test for null

}

ClientGo = false;

gplayer = "blue";

out.flush();

}

if (lostcon == null) {

incoming.close();

// null = lost connection

break; // client closed connection

}

}

incoming.close();

s.close();

}

} catch (Exception e) {}

}

Add widgets simply adds components to board.

A connection is still detected even when none of the swing components show.

I would be very greatful for any help!!>

[2912 byte] By [Mist0rza] at [2007-11-26 17:51:23]
# 1

When you write Java GUI application, do any I/O including socket interactions with a separate thread otherwise your I/O would semi-permanently steal the control from UI process. So your GUI component would never show up or hangs.

Read: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

The invokeAndWait() call in your current code is just useless. Don't do it.

hiwaa at 2007-7-9 5:03:54 > top of Java-index,Java Essentials,Java Programming...