0

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

When it is working

Not working

When it is 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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • *I am using "dialog" so I can keep the optionDialog on top of all windows.* - what does that mean? The dialog isn't even visible, A JDialog doesn't show up on the task bar. Usually you would use a visible JFrame as the parent component so you can click on the icon on the task bar if the option pane ever gets hidden. – camickr Jun 30 '20 at 03:49
  • I am using the JDialog that I set always on as the parent component so the optionDialog stays in front at all times. I am doing this for my own purposes I simply do not want it to go behind other windows and just click the task bar for it to come to the front. – ColemanDunn Jun 30 '20 at 03:54
  • @ColemanDunn Why don’t you use a custom `JFrame` or `JDialog` then.? `JOptionPane` was designed to be used in combination with an already visible window as its parent. – weisj Jun 30 '20 at 10:34
  • I understand. I am questioning how it works if the dialog isn't even visible as indicated in the code you posted. I'm wondering how a component can "always be on top" it it isn't even visible. I am also suggesting a different approach to see it if solves the problem or if you still have the same problem. That is I'm trying to narrow down where the problem is. You already posted this question days ago and got no solutions. So maybe you need an alternative approach? – camickr Jun 30 '20 at 13:42
  • @weisj I am using a Visible JDialog elsewhere in my code I can’t try that and get back – ColemanDunn Jul 01 '20 at 03:54
  • @camickr I am doing it this way just because that is what I saw on another stack overflow to always keep an optionpane on top. The JDialog isn’t necessarily what I want to be on top, the option pane is, but it is using the JDialog that is set to always be on top in order to do so. That way I see it is that it doesn’t have to be visible in order for it to be used. If you copy and paste the code it works as far as always being on top. What alternative approach are you suggesting exactly? That the JDialog be set so visible at some point? – ColemanDunn Jul 01 '20 at 04:02
  • @weisj no luck using an already visible dialog. – ColemanDunn Jul 02 '20 at 18:51
  • @camickr any other suggestions? – ColemanDunn Jul 02 '20 at 18:51
  • No. I don't use a Mac. I fail to see why the code you posted only works 80% of the time. Other Mac users will have to test to see if they have the same problem. – camickr Jul 02 '20 at 19:09

0 Answers0