amjazzed
amjazzed

Reputation: 29

Java GUI using setVisible()

I have a class that extends from JFrame like this:

public class TicTacToeSubMenu extends JFrame implements ActionListener { }

and in the same file I have another class for the client connection like this:

class ListenThread extends Thread { }

the thing is that in the ListenThread class I am trying to exit the current frame and open a new frame, so far the new frame opens as wanted , but the current frame won't exit.

I used both commands and neither are working:

1. TicTacToeSubMenu.this.setVisible(false);

2. setVisible(false);

how can I exit a current frame from another class at the same file?

a bigger exampel:

from a previous widnow frame i do this:

            TicTacToeSubMenu win = new TicTacToeSubMenu(word, false);
            win.setTitle("Tic Tac Toe");
            win.pack();
            win.setLocation(600, 200);
            setVisible(false);
            win.show();

then in a new file and new frame :

public class TicTacToeSubMenu extends JFrame implements ActionListener {
    variables.....

public TicTacToeSubMenu(String username, boolean playing) {
    new TicTacToeSubMenu();
}


public TicTacToeSubMenu() {
    connectUser();
}

public void connectUser() {
    clientThread = new ConnectThread();
    clientThread.start();

}

class ConnectThread extends Thread {
    InputStream input;
    OutputStream output;
    ObjectOutputStream oos;
    Socket s;

    public void sendText(String text) {
        try {
            oos.writeObject(text);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            s = new Socket(HOST, PORT);
            output = s.getOutputStream();
            oos = new ObjectOutputStream(output);

            isOnline = true;
            isConnected = true;

            new ListenThread(s).start();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class ListenThread extends Thread {

    Socket s;
    InputStream input;
    ObjectInputStream ois;

    public ListenThread(Socket s) {
        this.s = s;
        try {
            input = s.getInputStream();
            ois = new ObjectInputStream(input);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void run() {
        while (isConnected) {
            try {
                final String inputMessage = (String) ois.readObject();
                System.out.println(inputMessage);
                String user;
                user = getWord(inputMessage, 0);
                String opponent;
                opponent = getWord(inputMessage, 1);
                String message = getWord(inputMessage, 2);


                if (!user.equalsIgnoreCase(un)
                        && opponent.equalsIgnoreCase(un)
                        {

                            TicTacToeMultiPlayer window = new TicTacToeMultiPlayer(
                                    un, user, t2, playing, turn);
                            window.setTitle("Tic Tac Toe");
                            window.setLocation(400, 100);
                            window.pack();
                            TicTacToeSubMenu.this.setVisible(false);
                            window.show();
                        }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

}

Upvotes: 0

Views: 934

Answers (1)

Niknea
Niknea

Reputation: 39

If you would like to "hide" a JPanel, use the code "this.dispose();" rather than setting visibility to false.

Upvotes: 1

Related Questions