void NewJDialogcallone(JFrame frame)
{
location = frame.getLocationOnScreen();
int x = location.x;
int y = location.y;
dialog.setLocation(x, y);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
dialog.setAlwaysOnTop(true);
dialog.addComponentListener(this);
}
public void componentMoved(ComponentEvent e,?????)
{
JOptionPane.showConfirmDialog (null,
"This is the \"Ok/Cancel\"message dialog box.",
"",
JOptionPane.OK_CANCEL_OPTION);
}
I want to use the frame object so that the dialog box moves relative to the parent frame a.k.a. I move the parent frame and the dialog box moves with it.I want to call dialog.setLocationRelativeTo(//parent frame object//), which is possible only if I have the parent frame object.
If there is any way to get this window behaviour, please help me.
You just need to add the keyword
finalin front of the method parameterJFrame frame.I would also recommend to avoid using this:
because it is really annoying for the user experience. Usually, this is the sign that you did not properly instantiated your dialog, ie, by passing the correct Frame/Dialog owner.
Here is an example of window location synchronization and without using setAlwayOnTop():