0

I have a JDialog that is opened with a button click in a JFrame. Every time I close the JDialog and click and open JDialog again, it moves upwards in the screen (about 30 pixels). I tried fixing the frame location, used setLocationRelativeTo(null) and also tried to fix it with Toolkit.getDefaultToolkit().getScreenSize(); but it doesn't work. It just goes upward until it is on top of screen. Why could that be?

    // Play Hook Frame
    hookDialog = new JDialog(frame, "Play Hook", true);
    hookDialog.setSize(450, 250);
    hookDialog.setLocation(dim.width / 2 - hookDialog.getSize().width / 2,
            dim.height / 2 - hookDialog.getSize().height / 2);
    hookDialog.getContentPane().setBackground(bgColor);
    hookDialog.setResizable(false);
    hookDialog.getContentPane().setLayout(null);

    ...

    JButton btnPlayHook = new JButton("Play Hook");
    customizeButton(btnPlayHook);
    btnPlayHook.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!isHookPressed) {
                JOptionPane.showMessageDialog(frame,
                        "Please press Find Hook button first", "Error",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                for (int i = 0; i < hooks.size(); i++) {
                    int intMin1 = (int) (hooks.get(i).getStartTime() / 60);
                    int intMin2 = (int) (hooks.get(i).getEndTime() / 60);
                    int intSec1 = (int) (hooks.get(i).getStartTime() % 60);
                    int intSec2 = (int) (hooks.get(i).getEndTime() % 60);
                    String min1 = intMin1 + " min";
                    String min2 = intMin2 + " min";
                    String sec1 = intSec1 + " sec";
                    String sec2 = intSec2 + " sec";
                    String elem = "Hook" + (i + 1) + ": " + min1 + " "
                            + sec1 + " - " + min2 + " " + sec2;
                    if (!listElements.contains(elem))
                    listElements.addElement(elem);
                }
                hookDialog.setVisible(true);
            }
        }
    });
  • Impossible to know without seeing some code. – Java42 Oct 26 '14 at 00:00
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Oct 26 '14 at 00:08
  • @Java42 I have added the relevant parts. – user3616495 Oct 26 '14 at 00:08
  • @Java42 The problem with requesting 'some code' is that the OP usually posts uncompilable code snippets and the snippet often does not contain the code responsible for the problem. – Andrew Thompson Oct 26 '14 at 00:09
  • 1
    `hookDialog.setSize(450, 250);` should be `hookDialog.pack();` after components are added. – Andrew Thompson Oct 26 '14 at 00:10
  • `hookDialog.getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 26 '14 at 00:11
  • Oh my.. `hookDialog.setLocation(dim.width / 2 - hookDialog.getSize().width / 2, dim.height / 2 - hookDialog.getSize().height / 2);` ..instead use.. `hookDialog.setLocationRelativeTo(null);` – Andrew Thompson Oct 26 '14 at 00:12
  • @AndrewThompson As I have said in the post, I also tried doing it. There was the same problem. I am going to read your post on layout subject, by the way. Thanks. – user3616495 Oct 26 '14 at 00:21
  • *"As I have said in the post, I also tried doing it."* 'It' being what, specifically? Now is a good time for more words, rather than less. – Andrew Thompson Oct 26 '14 at 01:30

2 Answers2

0

This will center the dialog on the screen.

Do this:

    hookDialog.setLocationRelativeTo(null);

If that doesn't work:

    hookDialog.getContentPane().setLocationRelativeTo(null);
Petro
  • 3,484
  • 3
  • 32
  • 59
-1

Try this code on your system. Add your logic to this base and see when it starts behaving poorly.

public static void main(final String[] args) throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            final JFrame frame = new JFrame();
            frame.setSize(300, 300);
            frame.setLocationRelativeTo(null);
            final JDialog hookDialog = new JDialog(frame, "Play Hook", true);
            hookDialog.setSize(100, 100);
            hookDialog.setLocationRelativeTo(null);
            hookDialog.setResizable(false);
            hookDialog.getContentPane().setLayout(null);
            final JButton btnPlayHook = new JButton("Play Hook");
            frame.getContentPane().add(btnPlayHook);
            btnPlayHook.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    hookDialog.setVisible(true);
                }
            });
            frame.setVisible(true);
        }
    });
}
Java42
  • 7,628
  • 1
  • 32
  • 50