From JFrame to JFrame

RdeLange
RdeLange used Ask the Experts™
on
In my application I want to start another JFrame.
The first may not be visible as you can see the second.
If the second is ready it must disappear en show the first one again.

setVisible(false);
new FilesEdt("update", true, prepareRecord());
setVisible(true);

seems not to work. Screen one does not disappear

who can help??

----
Is a JDialog the only solution? The FiledEdt indeed shows a new JFrame (a window where the user has to give in data that is written to a file)
Does that mean that I only make a Frame if it may run next to the prior JFrame??
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This is because your JFrame is not a "modal" dialog box.

Commented:
In other words... JFrame 1 is still running while you start JFrame 2... so your code says...

Hide me
new FilesEdit (probably shows another frame from what I can guess of your message)
While that other windo is still working
show me.

The hide and show happen so quickly that it appears you never hide it in the first place.


I do not know what you are trying to do, but the most likely solution is to use a JDialog and pass "true" to the modal argument.

This will make frame 1 pause while jdialog (2) executes.


Author

Commented:
Is a JDialog the only solution? The FiledEdt indeed shows a new JFrame (a window where the user has to give in data that is written to a file)
Does that mean that I only make a Frame if it may run next to the prior JFrame??
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Commented:
Here is a try:

JFrame 1:
On the button that enables your JFrame 2, use setEnabled(false)  (this should not hide it, just render it inoperable, you can hide it if you prefer, same results).
Then start or show your JFrame2, and pass it the parent(JFrame 1) as a parameter.  This will be an instance variable of your JFrame2 called parrent.


On the JFrame2.  on your close button
parent.setEnabled(true); // or un-hide your panel if you prefer.

That's just about what a JDialog does.

-----
Alternatively, on the JFrame2, if you want to use the X button on top right, you should be able to use

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
parent.setEnabled(true);
}
}


Greetings,

   here is the example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FilesEdt extends JFrame implements ActionListener
{
     JFrame parent;

     public FilesEdt( String action, boolean ag1, Object ag2, JFrame parent )
     {
           this.parent = parent;

           JButton b = new JButton("Close");
           JFrame own = this;

           b.addActionListener( this );

           JPanel panel = new JPanel();
           panel.add ( b );

           setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
           setTitle( "Child Window" );
           getContentPane().add ( panel );
           setBounds( 200 , 0, 200, 200 );
           show();
     }

      public void actionPerformed ( ActionEvent e )
      {
           if ( e.getActionCommand().equals("Close") )
           {
                parent.setVisible ( true );
                setVisible(false);
          }
      }

}


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TwoFrames extends JFrame implements ActionListener
{
     public TwoFrames()
     {
           JButton b = new JButton("Open");
           JFrame own = this;

           b.addActionListener( this );

           JPanel panel = new JPanel();
           panel.add ( b );

           setTitle("Two Frames Test");
           getContentPane().add ( panel );
           setBounds( 0, 0, 200, 200 );
           show();
      }

      public void actionPerformed ( ActionEvent e )
      {
           if ( e.getActionCommand().equals("Open") )
           {
                new FilesEdt("Update", true, null , this );
                setVisible ( false );
          }
      }

      public static void main ( String argv[] )
      {
           new TwoFrames();
      }
}


hth,

Commented:
Excellent example!
RdeLange:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for kokchoon78

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial