Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set location of JDialog relative to JFrame

Is there a way to set a dialog location relative to a JFrame?

I would like to center the dialog to the frame that houses my GUI, instead the dialog often appears in the center of the screen rather than within the GUI.

like image 779
Itchy Nekotorych Avatar asked Apr 23 '12 15:04

Itchy Nekotorych


People also ask

Where is JDialog used in an application?

JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.

What is the difference between JFrame and JDialog?

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).

How do I open a JFrame in center?

By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.

What is JDialog in Java?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.


3 Answers

Is there a way to set a location relative to another JFrame

You can:

  • pass JFrame as argument for JDialog.setLocationRelativeTo(Component c)
  • set to desired Point from JFrame to JDialog.setLocation(Point p)

EDIT

all Swing code must be done on EventDispatchThread, meaning that setVisible(true) should be wrapped into invokeLater

EDIT2

when using the JFrame as Component c I am getting a cannot find symbol error

not possible, some small mistake

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ClosingFrameDialog extends JFrame {

    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenuBar MenuBar;
    private JMenu File;
    private JMenuItem Exit;
    private JMenuItem ShowDialog;
    private JDialog dialog;
    private Point location;

    public ClosingFrameDialog() {
        Exit = new JMenuItem(" Exit ");
        Exit.addActionListener(new ExitListener());
        ShowDialog = new JMenuItem(" Show Dialog ");
        ShowDialog.addActionListener(showingDialog());
        File = new JMenu(" File ");
        File.add(Exit);
        File.add(ShowDialog);
        MenuBar = new JMenuBar();
        MenuBar.add(File);
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createDialog();
            }
        });
    }

    private void createDialog() {
        JButton btn = new JButton(" Save changes ");
        btn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //some stuff for saving whatewer programatically
                dialog.setVisible(false);
            }
        });
        JButton btn1 = new JButton(" Ignore changes ");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(false);
            }
        });
        dialog = new JDialog();
        dialog.add(btn);
        dialog.add(btn1);
        dialog.setVisible(false);
        dialog.setAlwaysOnTop(true);
        dialog.setModal(true);
        dialog.setLayout(new GridLayout(2, 0, 10, 10));
        JPanel pane = (JPanel) dialog.getContentPane();
        pane.setBorder(new EmptyBorder(10, 10, 10, 10));
        dialog.addWindowListener(closeListener);
        dialog.pack();
    }
//
    private WindowListener exitListener = new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == 0) {
                System.exit(1);
            }
        }

        @Override
        public void windowIconified(WindowEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == 0) {
                //System.exit(1);
            }
        }
    };
//
    private WindowListener closeListener = new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            int confirm = JOptionPane.showOptionDialog(dialog,
                    "Are you want to save changes",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == 0) {
                //some stuff for saving whatewer programatically
                dialog.setVisible(true);
            } else if (confirm == 1) {// nothing only hide JDialog
                dialog.setVisible(true);
            }
        }
    };

    private Action showingDialog() {
        return new AbstractAction("Show Dialog") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                Runnable doRun = new Runnable() {

                    @Override
                    public void run() {
                        dialog.setVisible(false);
                        location = frame.getLocationOnScreen();
                        int x = location.x;
                        int y = location.y;
                        //dialog.setLocation(x, y);
                        dialog.setLocationRelativeTo(frame);
                        dialog.setVisible(true);
                    }
                };
                SwingUtilities.invokeLater(doRun);
            }
        };
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == 0) {
                System.exit(1);
            }
        }
    }

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

            @Override
            public void run() {
                ClosingFrameDialog cf = new ClosingFrameDialog();
            }
        });
    }
}
like image 194
mKorbel Avatar answered Oct 20 '22 01:10

mKorbel


The method you want is: setLocationRelativeTo()

Adding it with null to the JFrame, will center it on the screen. Adding the JFrame to the dialog will center it on the jframe.

Cheers.

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

public class Centered
{
    public static void main( String args[] )
    {
        JFrame jFrame = new JFrame();

        jFrame.setSize( 250 , 250 );
        jFrame.setLocationRelativeTo( null );
        jFrame.setVisible( true );

        JDialog jDialog = new JDialog();
        jDialog.setLocationRelativeTo( jFrame );
        jDialog.setVisible( true );
    }
}
like image 31
Jonathan Payne Avatar answered Oct 20 '22 01:10

Jonathan Payne


You should give this as the argument for the parent parameter when you construct the dialog (when you call it from a JFrame class that is usually your mainform). Unless you give code in your question, I cannot give more detailed help...

Edit: To center it within the parent, do this:

 MyDialog dialog = new MyDialog(this, true); //or false for non-modal
 dialog.setLocation(this.getWidth/2 - dialog.getWidth/2, this.getHeight/2 - dialog.getHeight/2);
 dialog.setVisible(true);
like image 3
MarioDS Avatar answered Oct 19 '22 23:10

MarioDS