JOptionPane window opens in background - java

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/

Related

how to make pop up overlay with my screen using java

Im trying to make a java program that will popup/notify me to stop what I am currently doing and do something. more like a reminder. My question is how will I make a pop up in java.
I found this docs, but dont know how to implement for the parent component.
JOptionPane.showMessageDialog(??,"this is a modal dialog.");
The answer depends, do you have a parent component (like a JFrame or other component) or are you simply display the JOptionPane independently?
If you are simply displaying the JOptionPane independently, you can simply pass it null.
If you are displaying the JOptionPane as part of a large application, with windows and other components, you can simply pass it a reference of what ever component you want the JOptionPane to displayed relative to, such as the window or most accessible container/component
The parent argument (if supplied) simply allows the dialog to act as a modal (blocking) dialog for the window which contains the supplied component. This requires the user to have to dismiss the dialog before they can continue to interact with the parent window/component
Take a closer look at How to Make Dialogs for more details

How to create a common static focusable window in Java?

I am creating a Transliterating tool in Java. It's almost complete.
Here is the screenshot.
I am using JWindow for dropdown, which must be focusable for some reason.
As, user can write in one input only at one time. I've created this window static, so all Text components use the same instance instead of creating new one.
Problem occurs, when I work in more than one window. It works fine unless both window are showing on screen. But when owner window of this dropdown window is closed, dropdown window is not focusable anymore.
As Javadoc of JWindow(Window owner) constructor says:
Creates a window with the specified owner window. This window will not be focusable unless its owner is showing on the screen. If owner is null, the shared owner will be used and this window will not be focusable.
So, how can I create a static, focusable window, that is shared by all components in different windows.
Don't use a JWindow.
Instead you can use a non decorated JDialog. Then you won't have the focus problem.
Edit:
You can prevent the dialog from initially getting focus when you make it visible by using code like:
dialog.setWindowFocusableState(false);
dialog.setVisible(true);
dialog.setWindowFocusableState(true);

i have displayed a JDialog using JOptionPane.showOptionDialog , can someone tell me how to set it inVisible or Dispose it?

i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required.
and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!
This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.
JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.",
"EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);
It sounds like you want to make it a JOptionPane.PLAIN_MESSAGE. That is what you need to put instead of whatever int2 is. I was going to link you to the tutorial but Space Pope already did that. You don't need to create a custom dialog to change the default icon, just change the message type to a plain message. The tutorial covers all this stuff.
You'll need to keep a reference to the dialog if you want to close it yourself. See the Oracle tutorial on custom Dialogs. The constructor you're using also puts in an icon by default; if you make your own dialog, you can control that part too.
JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:
JOptionPane optionPane = (JOptionPane)
SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
optionPane.setValue(JOptionPane.CLOSED_OPTION);
If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:
Window window = (Window) button.getTopLevelAncestor();
window.dispose();

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.

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