Joption pane location relative to its parent - java

I have a JOptionPane that checks for confirmation when a it's parent(JPanel) is closed.The problem is that when the parent is minimized and I close it from the start bar the confirmation Dialog is shown on the top left side of the screen not centered on the screen as it does when the parent is maximized.I assume that it gets its location based on it's parent,can there be some other reason for this and what fixes do you recommend? Thank you!

Related

Layout for Java Multi window appliciation

I need to know how to select a layout for a java swing application with multiple windows.
It has a dashboard kind home or main window with few icons(12 or more) on it. Clicking on an icon it will open a complex window on top of it then the main window is no longer visible. There will be another home icon to get back to main window on new view. Complex in the sense the opened window will have a tabbed layout. What i need to know is what layout should I use for this purpose.
Card layout and layered layout are the candidates I suppose. Or should I use separate frames or is there some other option available.
If the window can take the full screen and position the icons on it zooming appropriately according to the screen size would be great.
I'm glad if you can provide me a reference to a sample code.
Thank you in advance for helping me out.
I suppose you want to hide home screen when user opens another screen, and show it again when user clicks something like "Home" button.
For similar thing, I used JLayeredPane. It essentially allows you to sat Z-order for your components. In this case, you would have a JPanel for each screen you want to show, and you need to place it inside JLayeredPane, with home screen being initially on top. When you want to show another screen, you set it's layer to be topmost.

JDialog appears in the top left corner of screen, when when I've set it's position to the center of its parent

Before I initialized my dialog as
addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add question);
and I set the location of the dialog to be at the center of its parent by calling:
addQuestionDialog.setLocationRelativeTo(this)
This works and displays the dialog at the center of its parent, however when I set the dialog to be a modal dialog, it completely ignores the set method and displays the dialog in the top left corner of my screen.
addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add question", Dialog.ModalityType.DOCUMENT_MODAL);
however when I set the dialog to be a modal dialog, it completely ignores the set method and displays the dialog in the top left corner of my screen.
The order of the code should be:
dialog.setLocationRelativeTo(..);
dialog.setVisible(true );
I'm guessing you are using:
dialog.setVisible(true );
dialog.setLocationRelativeTo(..); // this is not executed until the dialog is closed.
This JDialog code sequence works for me:
setModal(true)
pack()
setLocationRelativeTo(frame)
setVisible(true)
Placing the setLocationRelativeTo(frame) line before the pack() line results in the off-center placement of the dialog. Interesting.:)

JDialog over Desktop area

I am trying to use a JDialog at the right of the screen, everything is almost perfect, but, if someone press the button on ther right end of the TaskBar, click on "Show Desktop area" my JDialog disappears I have to use ALT + TAB to get it back in in front. I can't set it AlwaysOnTop because I use other 3rd party programs that are fullscreen.
I tried:
jdigCentral.setAutoRequestFocus(true);
jdigCentral.setAlwaysOnTop(true);
and others, but without success
How can I have my JDialog to stay over just the Desktop Area?
Is jdigCentral your dialog box? If so then try this:
jdigCentral.setModal(true);
That will keep the dialog box on top of the parent JFrame. But I'm confused about the use of the word Desktop Area. Do you mean you want to keep the dialog box on top of the parent frame, or do you want it to always show on top of all other programs running on your desktop?

Clicking while holding the left click in SWING

I am writing a game in java and it has a grid view where you can left click on a single box (the box becomes red). How can make it so that after left clicking on a box if I keep pressing the left click and move the cursor onto another box it will keep clicking. Any suggestions ? I hope I was clear. Thanks.
You need to add a MouseMotionListener to your component.
The mouseDragged method will be called when the user moves the mouse whilst holding a mouse button down. You can then check if the cursor is over another box and fill it red.

What is parent Component for in JFolderChooser.showOpenDialog

Case 1:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component
Case 2:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);
What is the practical difference between the two cases?
Checkout the Javadoc for JFileChooser
The parent argument determines two
things: the frame on which the open
dialog depends and the component whose
position the look and feel should
consider when placing the dialog. If
the parent is a Frame object (such as
a JFrame) then the dialog depends on
the frame and the look and feel
positions the dialog relative to the
frame (for example, centered over the
frame). If the parent is a component,
then the dialog depends on the frame
containing the component, and is
positioned relative to the component
(for example, centered over the
component). If the parent is null,
then the dialog depends on no visible
window, and it's placed in a
look-and-feel-dependent position such
as the center of the screen.
internally it tries to get a window using the parent using this JOptionPane.getWindowForComponent(parent). Which in turn checks if parent is null or not...
if (parentComponent == null)
return getRootFrame();
If it is null then Root level frame is returned as parent container.
Using the internal SwingUtilities.getSharedOwnerFrame(). The javadoc for SwingUtilities.getSharedOwnerFrame() says...
Returns a toolkit-private, shared,
invisible Frame to be the owner for
JDialogs and JWindows created with
null owners.
You can specify the parent to determine which component the dialog is related to. It will determine the position of your dialog (centered, relative to the parent). I also guess that the dialog will be modal, thus blocking the parent window.
If you specify null, the dialog shown won't belong to any component, and I guess it will be displayed either at the top left of the screen or at the center (the last being more likely to happen, I have not tested).
Hop this helps !

Categories

Resources