I am using Netbeans 7.1
I have 2 jFrame
JFrame1 and JFrame2
I just not able to close or hide the JFrame1 when I switch to JFrame2

This is the JFrame1 Code -

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
            int flag=0;
            String sql="Select * from login;";
            smt1=con.createStatement();
            rs=smt1.executeQuery(sql);
            String log=jTextField1.getText();
            String pass =new String(jPasswordField1.getPassword());
            while(rs.next())
            {
                if(log.equals(rs.getString(1)) && pass.equals(rs.getString(2)))
                {
                    flag=1;
                    break;
                }
            }
            if(flag==1)
            {
                jLabel3.setEnabled(false);
                jLabel4.setEnabled(false);
                jTextField1.setEnabled(false);
                jPasswordField1.setEnabled(false);
                jButton1.setEnabled(false);
                jButton2.setEnabled(false);
                JFrame1 nf=new JFrame1(); // **SECOND JFRAME WHICH IS OPENED** //
                nf.setVisible(true);
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Error", "Please check user name / password",JOptionPane.ERROR_MESSAGE);
            }
        }
        catch(Exception ex){

        }

    }

But now I am not able to hide the JFrame1 once JFrame2 is shown.
Please show some way

just call setVisible(false); for the first JFrame

you have to do 2 things

1 create second frame constructor by passing first frame reference as shown bellow

JFrame1 nf=new JFrame1(first_frame_ref);

and there(i mena in second frame) you can call first frame to display whenever you want

  1. before calling second frame to display make first frame visibily as "false"

thats it

@radhakrishna I already tested your suggested code before posting. Problem is this code creates a new object of JFrame1 but the alredy created JFrame1 object still active. Is there any other way

If you want to hide or dispose the frame, use built in methods of java.

As all of said

First_frame_Obj.setVisible(False) -- if you want to hide and revoke later
First_frame_Obj.dispose() -- if you want to remove it from memory.

You can add this thing at when you are displaying your second frame.

Thanks

From the API doc...

public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

Really speaking it so confusing I have 1 login frame when user logged in then they will get the actual jframe where they can do other operations like database management but the login screen should not be displayed. From the Database Management screen they may go on other frames where other operation take place.

Is this possible with Dialog? Or JFrame is the correct way?

Is this possible with Dialog?

As mKorbel said in this post, there are two proper ways:

  • CardLayout
  • JDialog

So yes, it is possible with a dialog.

Or JFrame is the correct way?

Typically you need at most one JFrame per application.
Let's take Microsoft Office Word as an example: you have one main window (this is analog to having one JFrame), and for some functionality there are separate small windows (these are called dialogs, and are analog to JDialog).

Thank you wvery body for helping me. But I have done this in different way - I passed the the current reference of JFrame1 to the constructor of JFrame2 while creating object of JFrame2 in Jframe1 and then I used hat refernce for hiding JFrame1.
JFram1 code -

NavigationFrame nf=new NavigationFrame(this);
                nf.setVisible(true);

JFrame2 code-

public NavigationFrame(login O) {
        O.setVisible(false);
        initComponents();   

i have a similar problem my java files are named home.java and control.java and the frame names are frame1 and frame2..........i want to switch from frame1 to frame2 on button click..pls help where should i place ur given code? thnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.