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);
}
}
});