1

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 ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mc Gyver Basaya
  • 143
  • 6
  • 16

1 Answers1

6

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

A.Sharma
  • 2,771
  • 1
  • 11
  • 24
  • Maybe you do not understand the answer, but it is the same one that many would give for this requirement. It is an obvious fit. If you followed the link I provided in comment, you might note it also mentioned (among many other possibilities) modal dialogs. – Andrew Thompson Jul 29 '16 at 15:41
  • the truth is I have a main jframe and if the mouse is not moving it will automatically show the login form and make the main jframe disabled. hope you understand what I mean.. sorry for my bad english :-) – Mc Gyver Basaya Jul 29 '16 at 15:43
  • Please provide the code that shows the creation of the login form then. Also update your question. The way your question is stated currently, is that you want to be able to open up one JFrame while making the other one disabled/non-clickable. If by disabled, you mean you don't want it to show up at all, just set it's visibility to false. – A.Sharma Jul 29 '16 at 15:48
  • thank you. after many tries it help me to understand what JDialog model is. – Mc Gyver Basaya Jul 30 '16 at 00:28