Example usage for javax.swing JDialog setLocationRelativeTo

List of usage examples for javax.swing JDialog setLocationRelativeTo

Introduction

In this page you can find the example usage for javax.swing JDialog setLocationRelativeTo.

Prototype

public void setLocationRelativeTo(Component c) 

Source Link

Document

Sets the location of the window relative to the specified component according to the following scenarios.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me to open dialog");
    button.addActionListener(e -> {//from w  ww . j  a va 2s.  co  m
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(new JLabel("A dialog"));
        dialog.pack();
        dialog.setVisible(true);
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int result = JOptionPane.showConfirmDialog(null, "Show over parent?");
    for (int i = 1; i < 4; i++) {
        JFrame f = new JFrame("Frame " + i);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Component parent = (JOptionPane.OK_OPTION == result ? f : null);

        f.setSize(400, 300);/*from   ww  w.  j ava  2  s .  c o m*/
        f.setLocationByPlatform(true);
        f.setVisible(true);

        JDialog d = new JDialog(f);
        d.setTitle("Dialog " + i);
        d.setSize(300, 200);
        d.setLocationRelativeTo(parent);
        d.setVisible(true);
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);/*from   w ww  . j a  v  a  2  s .c  o  m*/
    JLabel jl = new JLabel();
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

    final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    Thread t = new Thread(new Runnable() {
        public void run() {
            dlg.setVisible(true);
        }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
        jl.setText("Count : " + i);
        dpb.setValue(i);
        if (dpb.getValue() == 500) {
            dlg.setVisible(false);
            System.exit(0);

        }
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    dlg.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Frame");
    frame.add(Box.createRigidArea(new Dimension(400, 300)));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();/*from  w w w  . j a  va 2  s  .c o  m*/
    frame.setVisible(true);

    JDialog dialog = new JDialog(frame, "Dialog", true);

    int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
    ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
    String enter = "enter";
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
    actionMap.put(enter, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    });

    dialog.add(Box.createRigidArea(new Dimension(200, 200)));
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDialog dialog = new JDialog(frame, false);
    dialog.setSize(200, 50);/*from  w  w  w . ja  v a2  s  .  c o m*/
    frame.addComponentListener(new ComponentAdapter() {
        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);
}

From source file:Main.java

public static void main(final String[] args) {
    JFrame frame = new JFrame("Frame");
    JDialog dialog = new JDialog(frame, "Dialog");
    frame.add(new JLabel("Content"));
    frame.addMouseListener(new MouseAdapter() {
        @Override//from w  ww .  j ava  2  s .  co  m
        public void mousePressed(MouseEvent arg0) {
            System.out.println("frame pressed");
            System.out.println("dialog focused " + dialog.isFocused());
            System.out.println("frame focused " + frame.isFocused());
            super.mousePressed(arg0);
        }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    dialog.add(new JLabel("Content"));
    dialog.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            dialog.requestFocus();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

private static JButton newButton(String label) {
    final JButton button = new JButton(label);
    button.addActionListener(e -> {/* w  ww . j  a  v a  2 s  . c om*/
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(newPane("Label"));
        dialog.pack();
        dialog.setVisible(true);
    });
    return button;
}

From source file:Main.java

public static void showDialog(JDialog dlg, Component parent, boolean resizable) {
    dlg.setResizable(resizable);/*  w w w  . j  a v  a 2 s  .c o m*/
    dlg.pack();
    dlg.setLocationRelativeTo(parent);
    dlg.setVisible(true);
}

From source file:Main.java

public static void showModalDialog(JDialog dlg, Component parent, boolean resizable) {
    dlg.setResizable(resizable);//from   www .  j av  a2s  . c o  m
    dlg.setModal(true);
    dlg.pack();
    dlg.setLocationRelativeTo(parent);
    dlg.setVisible(true);
}

From source file:de.dakror.virtualhub.server.dialog.BackupEditDialog.java

public static void show() throws JSONException {
    final JDialog dialog = new JDialog(Server.currentServer.frame, "Backup-Einstellungen", true);
    dialog.setSize(400, 250);//from w  w w. j a  v  a  2 s. c o m
    dialog.setLocationRelativeTo(Server.currentServer.frame);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    JPanel cp = new JPanel(new SpringLayout());
    cp.add(new JLabel("Zielverzeichnis:"));
    JPanel panel = new JPanel();
    final JTextField path = new JTextField((Server.currentServer.settings.has("backup.path")
            ? Server.currentServer.settings.getString("backup.path")
            : ""), 10);
    panel.add(path);
    panel.add(new JButton(new AbstractAction("Whlen...") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser jfc = new JFileChooser((path.getText().length() > 0 ? new File(path.getText())
                    : new File(System.getProperty("user.home"))));
            jfc.setFileHidingEnabled(false);
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.setDialogTitle("Backup-Zielverzeichnis whlen");

            if (jfc.showOpenDialog(dialog) == JFileChooser.APPROVE_OPTION)
                path.setText(jfc.getSelectedFile().getPath().replace("\\", "/"));
        }
    }));

    cp.add(panel);

    cp.add(new JLabel(""));
    cp.add(new JLabel(""));

    cp.add(new JButton(new AbstractAction("Abbrechen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    }));
    cp.add(new JButton(new AbstractAction("Speichern") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (path.getText().length() > 0)
                    Server.currentServer.settings.put("backup.path", path.getText());
                dialog.dispose();
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }));

    SpringUtilities.makeCompactGrid(cp, 3, 2, 6, 6, 6, 6);
    dialog.setContentPane(cp);
    dialog.pack();
    dialog.setVisible(true);
}