Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Frame Disappears when trying to pop up a message box?

I have a java code that responds to a button click event. The Parent JFrame which has the button Disappears when i try to pop up a message box , So what's the problem ?

Here are the codes :

private void TestConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     

    InitDatabaseObject();

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run()
        {
            if(Database.ConnectToDatabase())
            {

                JOptionPane.showMessageDialog(null,"Connection succeeded");

            }else{

                JOptionPane.showMessageDialog(null,"Connection failed");
            }
        }
    }); 



    Database.CloseDbConnection();

} 

Help me plz ..

like image 573
Hatem Avatar asked Dec 31 '25 05:12

Hatem


1 Answers

JOptionPane.showMessageDialog(null,"Connection succeeded");

try to use reference to your JFrame (which you don't want to disappear) instead of null

JOptionPane.showMessageDialog(frame,"Connection succeeded");
like image 182
Jan Vorcak Avatar answered Jan 02 '26 17:01

Jan Vorcak