What is parent Component for in JFolderChooser.showOpenDialog - java

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 !

Related

Java Swing: ScrollableTooltip vs ContextMenu

I'm working on a (legacy) Java-Swing application and ran into a problem:
The main GUI-component in this application is a Gantt chart, that displays tasks, as rectangles with a label, basically.
A right-click on a task opens a context-menu, while hovering over a task with the mouse will show a customized, scrollable JTooltip. This constellation leads to my problem.
If I position the tool-tip too far away from the current mouse position, the tool-tip disappears, before I can move the mouse to one of the scroll-bar handles (horizontal/vertical).
If I position the tool-tip too near to the current mouse position, the context menu won't open anymore, because the tool-tip hides the underlying task and the right-click is therefore captured by the tool-tip and not the task.
What I've tried so far:
searched for some kind of delay in ToolTipManager, to control how long the tool-tip is shown, after the mouse leaves the control, which triggered the tool-tip to be shown. As far as I can say, there is no such delay-property.
tried to find the right distance between tool-tip and current mouse position, so that the scroll-bar handles of the tool-tip can be reached and the context-menu is also shown. -> I found some distance, where both works, but often you have to try several times, until one can reach the scroll-bar handles.
So my question is:
Is there any way to control when a JToolTip is hidden after the mouse leaves the corresponding component?
create JWindow, better undecorated JDialog with correct modality (then could be easiest to catch MouseEvents)
only one window with setDefaultCloseOperation-DO_NOTHING_ON_CLOSE or HIDE_ON_CLOSE, by toggling only with setVisible false / true, to reuse this container for whole JVM instance, clear windows content before is setVisible(false) called
put there Swing Timer with (for example) 5-10 seconds for logical autoclosing, by testing SwingTimer.isRunning, if Mouse Scrolling continues and SwingTimer.isRunning returns true then to call SwingTimer.restart
override mouseClicked for whole JVM instance e.g.
if (window.isVisible)
window.getContentPane.removeAll()
window.setVisible(false)
else
someThingWithRealEventFromMouseListener
there can be used some of better Listener that returns Boolean value instead of using low level instance of MouseListener
you can to (re)dispatch() mouse scrolling (only inside of Bound of the current parent - JFrame, JDialog) to the popup window, by using two - three methods from SwingUtilities

JOptionPane window opens in background

I am developing a swing application, just a little query about JOptionPane.showMessageDialog() which is bugging me:
JOptionPane.showMessageDialog(null, "Record entered successfully");
If i write this code the Message window appears at the back of my parent frame.
JOptionPane.showMessageDialog(this, "Record entered successfully");whereas this code automatically places the window over the parent frame.
The question is: while implementing null as the first argument i get the message at the background of current parent frame whereas if i write this as the first argument the window comes over the parent frame. Why is this happenning?
In the method
showMessageDialog(Component parentComponent, Object message)
the first argument sets the parent of the dialog:
parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
I assume that the method appears inside a JFrame class, in which case passing this as the argument will set the parent component as that frame.
The java keyword this is used (in this case) to refer to the current class - so you're referring to your parent window. See this link, It's pretty handy:
http://javapapers.com/core-java/explain-the-java-this-keyword/

Joption pane location relative to its parent

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!

Child Frame calling a parent frame. Code Design

I have a Code design question. Let me explain the scenario and then the question. I have 2 Panels below
Basically, the first panel on clicking start, opens the second panel. On the Second panel when the file is chosen (background blue). That file needs to be opened in the editor pane of the first panel.
The question is: What is the good practice to do it. Both my panels are separate classes. Currently the second panel keeps a reference to the first panel. And on selection, calls the respective method of the first class to output the csv. This is a bad design anyday.
How should the children frames refresh an aspect of the parent frame?
use JDialog(parent, modal) instead of JFrame, could be depends of modality
create JDialog only one time, reuse this container for another event came from parent
JDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE), then every next actions are only about setVisible(true/false) wrapped in invokeLater()
then return (check if contents changed, changes back to the parent
you can to put there verifications by users action from JOptionPane("DYW to save changes ??"))

how to set position of JOptionPane

I'm creating this JOptionPane
JOptionPane.showMessageDialog(this, "File was saved", "Save",
JOptionPane.INFORMATION_MESSAGE);
but my JFrame is big so it is scrollable. When I call this command, a window is created in the bottom right corner and I can only see the header. How I can change the position of this JOptionPane?
According to the api 1.6:
the first parameter is parentComponent:
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
So there isn't no parameter to set the position of the JOptionPane, but you could at least pass null as first parameter to be sure your JOptionPane is well visible and centered.
You could create a JDialog out of a JOptionPane (see the JOptionPane API to see how to do this), and then display it anywhere you'd like as you can with any JDialog. By the way, perhaps you want to make your JFrame smaller by using JTabbedPanes or CardLayout so you don't have this problem.

Categories

Resources