Hatem
Hatem

Reputation: 493

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 ..

Upvotes: 3

Views: 977

Answers (2)

THelper
THelper

Reputation: 15619

Your dialog is not "attached" to the JFrame. Change your code to

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

where jframe is a reference to your JFrame object.

Upvotes: 2

Jan Vorcak
Jan Vorcak

Reputation: 20019

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");

Upvotes: 4

Related Questions