This is not a repeat of JDialog popup too small
I have a JOptionPane that stays up until the user presses Yes. My problem is that it displays correctly about 80% of the time and the other 20% it is extremely tiny and only big enough to show the three window options as shown here:
Working
Not working
here is my code:
import javax.swing.*;
public class JOptionProblem {
public static void main(String[] args) {
new JOptionProblem();
}
public JOptionProblem(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
int result = -5;
while (result != 0) {
String[] options = {"Yes", "No", "Cancel"};
result = JOptionPane.showOptionDialog(dialog, "message",
"Click a Button",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
}
}
}
);}
}
Things I have tried are:
UIManager.put("OptionPane.minimumSize",new Dimension(500,500));
as shown here: JOptionPane bigger
and
//static and final because no reason not to be. Insert this at the class definition
static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();
...
//I'd also make this static and final and insert them at the class definition
int dialogWidth = SCREEN_DIMENSION.width / 4; //example; a quarter of the screen size
int dialogHeight = SCREEN_DIMENSION.height / 4; //example
...
int dialogX = SCREEN_DIMENSION.width / 2 - dialogWidth / 2; //position right in the middle of the screen
int dialogY = SCREEN_DIMESNION.height / 2 - dialogHeight / 2;
dialog.setBounds(dialogX, dialogY, dialogWidth, dialogHeight);
as shown here: JDialog popup too small
It actually behaves normally if I use null as the parent component (the first parameter) but I am using "dialog" so I can keep the optionDialog on top of all windows.
If anyone can explain how to fix this and why it behaves irregularly it would be greatly appreciated.