How to set a pop-up frame always on the top?

In my program, if I click a button, it will pop-up a small frame-based window and I want to set it always on the top. How can I do that?

Assumed the frame-based window looks like:

...
JFrame myFrame = new JFrame();
myFrame.setVisable(true);
...

Besides, if I click the OK-button on this window, it will be closed or distroyed, not using setVisable(false). How to do that?
chenweiAsked:
Who is Participating?
 
DrWarezzConnect With a Mentor Commented:
To set it to be on top, try this:

myFrame.toFront();

should work. :)

gL,
[r.D]
0
 
gdrnecCommented:
Do you mean you want the Frame to act as a modal dialog?
0
 
armoghanCommented:
JDialog myDialog = new Dialog();
myDialog.setModel(true);
myDialog.show();
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
chenweiAuthor Commented:
To DrWarezz:

It works.

Bur how about the followings:
Besides, if I click the OK-button on this window, it will be closed or distroyed, not using setVisable(false). How to do that?
0
 
DrWarezzCommented:
Hi chenwei,
I'm not totally sure what you mean, by:
"not using setVisable(false)"

Could you elaborate a little more? Thanks.
[r.D]
0
 
chenweiAuthor Commented:
Hi DrWarezz,

after I click the OK-button on this frame, this frame-window should close, i.e. should be deleted from the program memory. Since I don't know how to do that, I use setVisable(false). But the frame-window is still in the program-memory, right?

Is there any function like myFrame.close() ?
0
 
armoghanCommented:
add the listener to the button and call this function
frame.dispose();

use like this for X button on toolbar
frame.setDefaultCloseOperation(
                                 JFrame.EXIT_ON_CLOSE);

 
 you can also handle the closing event like this
 frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
///Do some stuff here before closing
            }
        });
0
All Courses

From novice to tech pro — start learning today.