How to disable main JFrame when open new JFrame

后端 未结 5 1466
旧时难觅i
旧时难觅i 2020-12-03 14:28

Example now I have a main frame contains jtable display all the customer information, and there was a create button to open up a new JFrame that allow user to create new cus

相关标签:
5条回答
  • 2020-12-03 14:51

    just use firstFrame.setVisible(false) on the first frame. This will make it hidden..

    if you want a more general approach you could have a reference to the current displayed frame somewhere and change it when a new frame requests to be shown

    JFrame currentFrame;
    
    void showRequest(JFrame frame)
    {
      currentFrame.setVisible(false);
      currentFrame = frame;
      currentFrame.setVisible(true);
    }
    
    0 讨论(0)
  • 2020-12-03 14:54

    I think you should use this code for the main jframe when you trying to open new one :

    this.setEnabled(false);

    0 讨论(0)
  • 2020-12-03 15:04

    Sorry for the late answer but have you considered the Singleton design pattern? It will return the same instance of a class whenever you want the class. So if the user wants a frame to enter the details, there will only be one frame open (same instance)

    It goes something like this:

    private static MySingleFrame instance = null; //global var
    
    private MySingleFrame() { } //private constructor 
    private static MySingleFrame getInstance()
    {
    
    if(instance == null)
    {
    instance = new MySingleFrame();
    }
    
    //returns the same instance everytime MySingleFrame.getInstance() is called
    return instance; 
    
    
    }
    
    0 讨论(0)
  • 2020-12-03 15:05

    I would suggest that you make your new customer dialog a modal JDialog so that you do not allow input from other dialogs/frames in your app while it is visible. Take a look at the modality tutorial for details.

    0 讨论(0)
  • 2020-12-03 15:11

    You can use:

     private void btn_NewFormActionPerformed(java.awt.event.ActionEvent evt) { 
    
                 this.hide();
                 new Frm_NewFormUI().setVisible(true);
    
     }
    
    0 讨论(0)
提交回复
热议问题