Link to home
Start Free TrialLog in
Avatar of buri
buri

asked on

JOptionPane position in a JDesktopPane within a JScrollPane

Hi,

I have implemented a scrollable workspace which is a JDesktopPane inside of a JScrollPane. When I use JOptionPane.showInternalXXXX() the option pane appears centered over the desktop pane, which may or may not be visible in the viewport of the scroll pane (the code that actually does this is in JOptionPane.createInternalFrame()). I would prefer to have the option pane appear centered over the visible viewport rather than the entire desktop pane but the modal nature of the option panes prevents me from repositioning anything after creation.

I have already tried overriding the createInternalFrame() method with one of my own, but modality requires package-private calls (JInternalFrame.startModal()) which cannot be accessed from my new method.

Is there a clean way to make an internal option pane centered on an arbitrary unit, rather than the highest JDesktopPane?

Thanks in advance.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You need to create your own option pane 'manually'.
Avatar of buri
buri

ASKER

Please elaborate on this statement.

I have tried creating a JOptionPane and placing it inside of a JInternalFrame, but this does not accurately mimick the behaviour of the modal JDialog. The JOptionPane's internal methods accomplisht his by calling package-private methods which I cannot access without placing my code in the javax.swing package.

Thanks.
BU
Create an JOPtionPane object directly via one of it's ctor. Then create your JDialog using the createDialog() method, and place this dialog where you require it.

JOptionPane pane = new JOptionPane(.....
JDialog dialog = pane.createDialog(viewport, "Title");
Avatar of buri

ASKER

There is no problem with the JDialog -- it's the INTERNAL variety that I need to circumvent. All of my external option panes function correctly.

Thanks.
BU
Sorry that should have said createInternalFrame(...
What do you pass as the frame parent?
Have you tried using the viewport?
Avatar of buri

ASKER

The parent must be a child of the desktop pane within the viewport, otherwise JOptionPane.createInternalFrame() will exit on a NPE at line 1118, where it tries to center on the desktop pane ancestor.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of buri

ASKER

This seems to work appropriately. Thanks for your help!