0
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.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Suraj B
  • 25
  • 1
  • 8

2 Answers2

2

You just need to add the keyword final in front of the method parameter JFrame frame.

 void NewJDialogcallone(final JFrame frame) 
 ...

I would also recommend to avoid using this:

dialog.setAlwaysOnTop(true);

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():

import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle("Test dialog synch");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // On the next line I pass "frame" to the dialog so that the dialog never
            // goes behind the frame, avoiding the need for setAlwaysOnTop
        final JDialog dialog = new JDialog(frame, false);
        dialog.setSize(200, 50);
        frame.addComponentListener(new ComponentAdapter() {

            private Point lastLocation;

            @Override
            public void componentMoved(ComponentEvent e) {
                if (lastLocation == null && frame.isVisible()) {
                    lastLocation = frame.getLocation();
                } else {
                    Point newLocation = frame.getLocation();
                    int dx = newLocation.x - lastLocation.x;
                    int dy = newLocation.y - lastLocation.y;
                    dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
                    lastLocation = newLocation;
                }
            }

        });
        frame.setSize(400, 200);
        frame.setVisible(true);
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • dialog.setAlwaysOnTop(true); keeps the dialog box above the frame. I want it to inhibit a behaviour wherein , when i move the frame{parent frame} , the Jdialog box in front of the parent frame should also move and be positioned relative to the frame. Can you suggest me something there? – Suraj B May 31 '12 at 12:50
  • Thank you so much , this was what i was looking for. A brilliant answer which solved my problem. I promise give you credit when i post this anywhere to pay it forward. – Suraj B May 31 '12 at 20:13
0

You can easily create a component listener which references whatever objects you need it to

    final Object one = new Object();
    final Object two = new Object();

    ComponentListener listener = new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            one.toString();
        }
        public void componentMoved(ComponentEvent e) {
            two.toString();
        }
        public void componentResized(ComponentEvent e) {
            one.toString();
        }
        public void componentShown(ComponentEvent e) {
            two.toString();
        }
    };
ewan.chalmers
  • 16,145
  • 43
  • 60