Every time I run this piece of code, the output is a dialog box that appears at the top left hand corner of the screen and the title does not show.
Is there any way that I can change this so that the dialog appears at the middle and of acceptable size?
Code:
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
final JDialog dialog = new JDialog(f, "Hello world", true);
Timer timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
System.out.println("Dialog closed");
}
}

Absolutely. By default, a
JDialog(or aJFrame) will appear like that. You need to set bounds on it:However, if you just set some magic numbers for it, this will not scale well to other systems. Instead, get the screen dimension, and set off of that:
Alternatively, if you add components to the
JDialog, then callthe dialog will now be the minimum size to accommodate the components. If you are using components that should be packed tight, use this method; then you don't have to painstakingly construct the right width and height by hand.