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?