Mc Gyver Basaya
Mc Gyver Basaya

Reputation: 153

set JFrame always on top and disable all JFrame behind it

I have 2 frames on my project, the 1 is my main frame and the 2nd one is the frame that only visible if I click the button.

display jframe.class when the button is click.

here is my code in my button action performed

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       jframe jf = new jframe();
          jf.setVisible(true);
          jf.setAlwaysOnTop(true);
    }  

This code works but the problem is I want the main frame disable or unclickable while the 2nd frame is visible ...

can I do that the same concept of JOptionPane ?

Upvotes: 1

Views: 3584

Answers (1)

A.Sharma
A.Sharma

Reputation: 2799

You are essentially talking about a modal. You should use a JDialog and set the modality to true while passing the JFrame in as a argument:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
    myFrame = new JFrame("Hello World");
    modal = new JDialog(myFrame, "This is a modal!", true);
    modal.setVisible(true);
    modal.setLocationRelativeTo(null); //Center the modal
}

You can find more documentation here:

https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

Upvotes: 6

Related Questions