tags:

views:

38

answers:

3

Hi,
I want to position a JDialog box below a JTextField of a JFrame and when the dialog box opens my JFrame should not able to move -- that is, it should not be draggable. Any suggestions?

+1  A: 
public class DialogTest {
    public static void main(String[] args) {

        final JFrame frame = new JFrame("Frame");
        JTextField field = new JTextField("Click me to open dialog!");
        field.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                JTextField f = (JTextField) e.getSource();
                Point l = f.getLocationOnScreen();

                JDialog d = new JDialog(frame, "Dialog", true);
                d.setLocation(l.x, l.y + f.getHeight());
                d.setSize(200, 200);
                d.setVisible(true);
            }
        });
        frame.add(field);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 100);
        frame.setVisible(true);
    }
}
dacwe
Forgot the not movable frame first time, fixed it now...
dacwe
Setting the dialog to modal wont make it immovable...
willcodejavaforfood
It worked... Thanks....
harishtps
@willcodejavaforfood: No, but the question was to make the frame immovable..
dacwe
OOOOOOOOOOOOOOOOOOOOOOOOOOPS :)
willcodejavaforfood
@harishtps, don't forget to accept the answer.
camickr
A: 

Use JDialog.setLocationRelativeTo to set it below the text field.

mdrg
That's not below?!
dacwe
A: 

Create a modal JDialog like this.

public class MyDialog extends JDialog
{
  private int width = 50;
  private int height = 50;

  public MyDialog(Frame parent, int x, int y)
  {
    super(parent, "MyTitle", true);
    setBounds(x, y, width, height);
  }

}

Being modal means the user won't be able to interact with the parent until the dialog has closed. You can figure out the location you want and pass the x,y coords in the constructor.

Qwerky