Search code examples
javaswingjframedispose

Why "dispose()" and "setVisible()" isn't close the form?


I got 2 forms from JFrame class. By button clicking, one of them must be closed and another opened. I've tried with "setVisible()" and "dispose()", results was the same - nothing didnt change. Below my code of classes with version with "dispose()"-try:

 public static void btnEnterHandler() throws Exception{
    JFrame mainFrame = Forms.mainFrameDraw(new Chat());
    JFrame enterFrame = Forms.enterFrameDraw(new Chat());
    try{
        enterFrame.dispose();
        mainFrame.setVisible(true);
    } catch(Exception ex2) {
        Logger.getLogger(enterFrame.getName());
    }
    //todo stopped here!
}

And class with forms:

public static JFrame mainFrameDraw(JFrame frame) {
    textAreaMain = new JTextArea(FRM_HEIGHT / 19, 50);
    textFieldMessage = new JTextField();
    textAreaMain.setLineWrap(true);
    textAreaMain.setEditable(false);
    JScrollPane spMain = new JScrollPane(textAreaMain);
    spMain.setLocation(0, 0);

    JButton btnSend = new JButton();
    btnSend.setText("Send");
    btnSend.setToolTipText("Broadcast a message");
    btnSend.addActionListener(e -> {
        try {
            Chat.btnSendHandler();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    });

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.setResizable(false);
    frame.getContentPane().add(BorderLayout.NORTH, spMain);
    frame.getContentPane().add(BorderLayout.CENTER, textFieldMessage);
    frame.getContentPane().add(BorderLayout.EAST, btnSend);
    frame.setVisible(false);

    return frame;
}

public static JFrame enterFrameDraw(JFrame frame){
    JButton btnEnter = new JButton("Sign in!");
    textFieldLogin = new JTextField("Login!");
    btnEnter.addActionListener(e -> {
        try{
            Chat.btnEnterHandler();
        } catch (Exception e2){
            e2.printStackTrace();
        }
    });


    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.getContentPane().add(BorderLayout.PAGE_START, textFieldLogin);
    frame.setResizable(false);
    frame.getContentPane().add(BorderLayout.PAGE_END, btnEnter);
    frame.setVisible(true);

    return frame;
}

}

It's not all code. But i think, it's enough. If you'll need more - give me know.


Solution

  • First of all, try to avoid using only static classes because it's actually not what's java about. Get some knowledge about SOLID principles, it will help you out so much.

    If it's about your case:

     JFrame mainFrame = Forms.mainFrameDraw(new Chat());
     JFrame enterFrame = Forms.enterFrameDraw(new Chat());
    

    On button click it seems like you are initializing completely new frames, and then immediately close one of them:

    enterFrame.dispose();
    

    What you want to do is have instance of your JFrame inside btnEnterHandler().

    Working example (but still please consider not using so many static!):

    public static JFrame enterFrameDraw(JFrame frame){
        //...
        btnEnter.addActionListener(e -> {
            try{
                Chat.btnEnterHandler(frame);
            } catch (Exception e2){
                e2.printStackTrace();
            }
        });
    

    Then your handler looks like:

    public static void btnEnterHandler(JFrame frame) throws Exception{
        JFrame mainFrame = Forms.mainFrameDraw(new Chat());
        try{
            frame.dispose();
            mainFrame.setVisible(true); //i'm pretty sure its not necessary because its visible by default
        } catch(Exception ex2) {
            Logger.getLogger(enterFrame.getName());
        }
    }