JOptionPane.showConfirmDialog exits the screen when the message is large

JOptionPane.showConfirmDialog(this, 
                              message,
                              "title",
                              JOptionPane.YES_NO_OPTION,
                              JOptionPane.WARNING_MESSAGE);

A message can be 10 lines, and a message can be 500 lines. It changes dynamically. I want to implement a scrollbar if the message exceeds the height of the screen.

So I tried:

JTextArea textArea = new JTextArea (message);
JscrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JOptionPane.showConfirmDialog(this,
                              scrollPane,
                              "title",
                              JOptionPane.YES_NO_OPTION,
                              JOptionPane.WARNING_MESSAGE);

This will open the dialog box and the scroll bar in the windows and it will work fine, but on mac os the dialog goes off the screen.

Can anyone help me?

+3
source share
1 answer

You can set the preferred size for scrollPanebefore showing the dialog to limit its size:

scrollPane.setPreferredSize(new Dimension(400, 200));

The size used may be based on the size of the screen, which you can get as follows:

Toolkit.getDefaultToolkit().getScreenSize()

Please note that I have not tested this on platforms other than Mac OS X.

0
source

All Articles