0

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();
            }

        }
    }
}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
amjazzed
  • 29
  • 7
  • Though the implementation of two `JFrame` is not a good practice, since not most many GUI have this feature. But on a side note, might be this [example](http://stackoverflow.com/a/9443609/1057230), be able to help in the direction :-) – nIcE cOw Jun 29 '14 at 02:49
  • i editd the post and added an example – amjazzed Jun 29 '14 at 04:11
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) For many (or 2) components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 29 '14 at 04:44
  • ANYWAY - I SOLVED THE PROBLEM by making a static JFrame tmp; and then in the construtor temp = this; and it worked.. – amjazzed Jun 29 '14 at 13:31

1 Answers1

1

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

Niknea
  • 39
  • 9