0

I have a jframe with one textfield and one button. When I tap on button I create a websocket and I receive datas from it. When data arrives I want to hide this Jframe and show another Jframe. For this reason I do in this way:

This is the first JFrame

public class PrimaSchermata {

    private JFrame mainFrame;
    private JPanel controlPanel;
    private Session sessione;
    private JTextField userText;


    public PrimaSchermata(){

          mainFrame = new JFrame("First Jframe");
          mainFrame.setSize(500,200);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.setResizable(false);

          controlPanel = new JPanel();
          controlPanel.setLayout(new GridBagLayout());

          this.showTextField();
          mainFrame.add(controlPanel);

          mainFrame.setVisible(true); 
    }
    private void showTextField(){

          JLabel  namelabel= new JLabel("Email: ", JLabel.RIGHT);

          this.userText = new JTextField(15);
          JButton loginButton = new JButton("Login");
          loginButton.addActionListener(new ActionListener() {

              public void actionPerformed(ActionEvent e) {     

                  PrimaSchermata.this.loginEvent(userText);

                }
          }); 


          controlPanel.add(namelabel);
          controlPanel.add(userText);

          controlPanel.add(loginButton);
          mainFrame.setVisible(true);  
       }
private void loginEvent(JTextField tokenText){

           if(tokenText.getText().length() == 0){

               JOptionPane.showMessageDialog(mainFrame, "Inserisci email", "", JOptionPane.ERROR_MESSAGE, null);
               return;

           }

           this.gestioneWebSocket();

       }
      private void gestioneWebSocket(){


            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            URI apiUri = URI.create("wss://websocketurltest");
            try {
                Session session = container.connectToServer(PrimaSchermata.class, apiUri);

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

     @OnOpen
        public void onOpen(Session session) throws java.io.IOException
        {

            System.out.println("websocket opened");
            session.getBasicRemote().sendText("logintest");

        }
       @OnMessage
        public void onMessage(String message)
        {
            System.out.println("Message received: " + message);

            if(message.length() > 0){

                System.out.println("login ok");
                this.mainFrame.setVisible(false);
                secondFrame x = new secondFrame();
                this.mainFrame.dispose();
            }

         }

This is the second JFrame:

public class secondFrame extends JFrame{

    public secondFrame() {

          setBounds(100, 200, 120, 120);
          setTitle("Second JFrame");
          setVisible(true);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

My problem is this:

when I tap on button and I receive data from websocket the second JFrame shows but the first JFrame doesnt disappear despite I set visible = false in the first JFrame.

Where I wrong? Or exist another way to achive this?

gianni
  • 113
  • 2
  • 9
  • 2
    Probably you might want to use [Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) and not multiple `JFrame`s, see [The use of multiple JFrames, good / bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (The general consensus says it is a bad practice). Instead build your GUI based on `JPanel`s not in `JFrame`, as the latter is a rigid container which doesn't allow to place it inside others, maybe a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) might help too – Frakcool Jun 14 '17 at 15:37

2 Answers2

0

Why not just reuse the same JFrame and just reset the view with the setContentPane(JComponent ). Do you necessarily need to have multiple JFrames?

Euler
  • 312
  • 1
  • 11
  • I need a new window, you know another way to do it. Sorry but it's just that I use swing – gianni Jun 14 '17 at 14:24
  • Make sure that your reference is still pointing to the JFrame that you want to close. setVisible() should work. There is a property that may need to be set on initialization with something close to setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) – Euler Jun 14 '17 at 14:45
  • This way doesnt work. Which way do you use to change the window? – gianni Jun 14 '17 at 14:52
  • All you do is call the setContentPane() with another JComponent. Hope this helps. – Euler Jun 14 '17 at 15:06
0

Try using mainFrame.dispose(); instead of setting visible to false, and do this before you open the second window or send the request.

CJ Burkey
  • 385
  • 1
  • 14