how to set position of JOptionPane - java

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.

Related

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/

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();

Java Swing - resize Dialog on JLabel.setVisible(...)

I have a Java Swing Dialog with a hidden JLabel above each input component (i.e. JTextField). The purpose of this hidden JLabel is to use it as validation output for its input component.
Let's say, there is an input field for the description of some entity, which has to be non empty, and should contain some special stuff. On error, the action could call the following method:
private void invalidateDescription(String errMessage) {
errDescriptionLabel.setText(errMessage);
errDescriptionLabel.setVisible(true);
descriptionTextField.setBackground(ERR_COLOR);
}
After that, I call pack() and invalidate()
The problem is, that the JDialog still has the same vertical size, so that some of the components (the buttons in the bottom of the dialog) disapear (because they're out of view).
Do you have any suggestion how to fix it?
Best Regards.
edit: I forgott to mention, that the JDialog has a "Free Design" Layout (Netbeans GUI Builder default).
edit 2: I'm looking for a solution which doesn't require kind of a placeholder for (error) JLabel. "Empty Space" is not a desired solution because the dialog doesn't look balanced.
Use CardLayout place your labesl and empty JPanels ans swap thm when necessary.
Instead of using setVisible(), give errDescriptionLabel a background color that matches that of the enclosing panel when the entry is valid.
I have a Java Swing Dialog with a hidden JLabel above each input component
I would not use a hidden component for this. I would change:
private void invalidateDescription(String errMessage)
to
private void invalidateDescription(String errMessage, component inputComponent)
Then I would display a popup with the error message. You could use a non-decorated JDialog as the popup. You might even be able to use a JPopupMenu as the popup.
When you display the popup you would position the popup releative to the input component.

Unable to focus JOptionPane on foreground with default frame

I have a piece of code in my program where in I need to display an error message. Code:
String ErrorMsg=" Error to be Diplayed ";
JOptionPane.showMessageDialog(null, ErrorMsg, "Failure", JOptionPane.ERROR_MESSAGE);
Note: Default frame is used.
The message is successfully displayed, but before acknowledging by pressing "OK" button if I try any other successful flow the message box control is lost and message box won't be on foreground blocking even the successive flows.
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden. How to do that?
If you want JOptionPane to behave as it would in a full-fledged GUI, then first create a full-fledged Swing GUI. Forget using "default" frames or whatever you're using (the console perhaps). You are desiring GUI behavior, and so to get this you must create a GUI by displaying your application in a JFrame and have the JFrame launch the JOptionPane.
Pass reference to the parent frame instead of the null (first param).
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden.
Use a JFrame and setAlwaysOnTop(true). You will need to display your own message and button.
A JOptionPane uses a JDialog behind the scenes. A JDialog does not support this property.
Edit:
To get the icon used by the option pane you can use:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
For a list of the other icons see: UIManager Defaults.
The below code is enough to make a JoptionPane message with default frame to be set on top .
JDialog dialog = new JOptionPane("ErrorMsg",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION).createDialog(" Failure");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();

How to make a resizable JDialog?

I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:
set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
make the showed dialog resizable.
My code looks like:
JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
panel,
"titel",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]);
I know that I could obtain the desired result building a JDialog this way:
JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);
The problem with the last solution is that I can't get the return value.
EDIT:
in response to #camickr observations:
Why do you need to set the preferred size? If you build the panel
properly is should be displayed at its preferred size.
I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?
Read the JOptionPane API. Search for "Direct Use". It shows you how to
directly access the dialog used by the option pane and you can
I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.
This hack using a HierarchyListener to get access to the JOptionPane's also works:
http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.
Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can
With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.
What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.
If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.

Categories

Resources