I am working on a program that shows the following menu (menu_image) when it starts. I have a little problem: I'd want to show it on the top of the other windows, but I am not able to achieve this.
class Menu {
public String showMenu(){
Object[] options = {"option1", "option2", "option3"};
Object selectionObject = JOptionPane.showInputDialog(null, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
String selectionString = selectionObject.toString();
return selectionString;
}
}
Can someone help me, please? Thank you in advance
Based on Berger's suggestion, I solved my problem in the following way...
class Menu {
public String showMenu(){
//i solved my problem adding the following 2 lines of code...
JFrame frame = new JFrame();
frame.setAlwaysOnTop(true);
Object[] options = {"option1", "option2", "option3"};
//...and passing `frame` instead of `null` as first parameter
Object selectionObject = JOptionPane.showInputDialog(frame, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
String selectionString = selectionObject.toString();
return selectionString;
}
}
Related
How can I create a MessageDialog that is resizable.
Shell parentShell = Display.getCurrent().getActiveShell();
MessageDialog.openInformation(parentShell, "title", "message");
The information dialog cannot be changed in size. How to make it resizable?
The MessageDialog is not meant to be resizable. If you really really want to make it resizeable, override the getShellStyle() method to return the desired style flags.
For example
MessageDialog dialog = new MessageDialog( shell,
"title",
null,
"message",
MessageDialog.INFORMATION,
new String[] { IDialogConstants.OK_LABEL },
0 )
{
protected int getShellStyle() {
return SWT.SHELL_TRIM;
}
};
will result in a resizable dialog with an information icon and min/max/close buttons.
In my application I use JOptionPane.showConfirmDialog() at several places.
I now have the requirement to prevent it from focusing a button, so that when a user presses <Enter>, the dialog remains.
Is there a way to do this without having to write my own extension of dialog?
To clarify: the main issue is that it may not close when the user presses enter. The dialog not focusing is merely an obvious way of accomplishing that.
If you set custom buttons and set the initial button value to null, it will give you the behavior you want: no initially focused button, so pressing the Enter key will not do anything.
See the below example:
public class Test {
public static void main(final String[] args) {
final Object[] options = { "OK", "Cancel" };
JOptionPane.showOptionDialog(null, "Enter won't work.", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options,
null); // this is the trick
JOptionPane.showOptionDialog(null, "Can press Enter.", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[1]);
}
}
I'd like to replace the icon in a JOptionPane title bar (as it currently shows the default Java coffee logo).
I tried the following:
JOptionPane.showMessageDialog(null, "Some Text", "Login",
JOptionPane.INFORMATION_MESSAGE, ImageCacheProvider
.instance.getImageIcon("img/an image.png"));
It replaces the icon in the window but not the one in the title bar:
Is there any approach to change the icon in the title bar or alternatively to hide the default Java icon without having to implement a JDialog class?
Thanks a bunch!
Thomas
Use it like this:
Icon icon = new ImageIcon("d:/temp/CheckBox.gif");
JOptionPane jp = new JOptionPane("Session Expired - Please Re Login"),
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
icon);
JDialog dialog = jp.createDialog(null, "Session Expired - Please Re Login");
((Frame)dialog.getParent()).setIconImage(((ImageIcon)icon).getImage());
dialog.setResizable(true);
dialog.setVisible(true);
This worked well for me:
private static final Image myImage = ...;
/*
* Copied from javax.swing.JOptionPane.showOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)
*/
#SuppressWarnings("deprecation")
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException {
JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue);
pane.setInitialValue(initialValue);
JDialog dialog = pane.createDialog(parentComponent, title);
// Added this line
dialog.setIconImage(myImage);
pane.selectInitialValue();
dialog.show();
dialog.dispose();
Object selectedValue = pane.getValue();
if (selectedValue == null)
return JOptionPane.CLOSED_OPTION;
if (options == null) {
if (selectedValue instanceof Integer)
return ((Integer) selectedValue).intValue();
return JOptionPane.CLOSED_OPTION;
}
for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
if (options[counter].equals(selectedValue))
return counter;
}
return JOptionPane.CLOSED_OPTION;
}
This is probably a dumb question, but I can't figure out how to fix it. I want all my JoptionPanes to be resizable, so I am imbedding them in JDialog. I will have to convert all my showXxxDialog calls eventually, so I decided to start with showInputDialog. Everything works (the Dialog looks nice, and is resizable), except that it won't display the initial value in the JOptionPane display, even though it is correct in the JOptionPane constructor. Here is my code (messageType is PLAIN_MESSAGE, but QUESTION_MESSAGE does the same):
public class MyOptionPane {
static Object showInputDialog(Object f, Object message, String title, int messageType,
Icon ico, Object[] options, Object initValue) {
JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION,
ico, options, initValue);
JDialog dialog = pane.createDialog((Component) f, title);
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
pane.setWantsInput(true);
dialog.pack();
dialog.setVisible(true);
return pane.getInputValue();
}
}
Help would be much appreciated!
I have good and bad news, a fix to your problem is to include the line: pane.setInitialSelectionValue(initValue);. Great right? Well the bad news is that I cannot explain why it doesn't auto insert the initValue via the constructor. Hopefully someone else can build off of this and explain to us both.
import javax.swing.*;
import java.awt.*;
public class MyOptionPane {
static Object showInputDialog(Object f, Object message, String title, int messageType,
Icon ico, Object[] options, Object initValue) {
JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION,
ico, options, initValue);
JDialog dialog = pane.createDialog((Component) f, title);
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
pane.setInitialSelectionValue(pane.getInitialValue()); // set it
pane.setWantsInput(true);
dialog.pack();
dialog.setVisible(true);
return pane.getInputValue();
}
}
I have a small problem trying to figure out how to check if a user presses a button in a custom JOptionPane.
My dialog is based on an inputDialog with custom texts for the YES, NO and CANCEL buttons ("Select", "Cancel", "Open Editor").
I tried searching for a solution, but all I found was questions that used the static JOptionPane functions.
Here is my code I am using for now:
public SelectItemDialog(Component parent) {
super("Please select an item:", YES_NO_CANCEL_OPTION, PLAIN_MESSAGE, Editor.getIcon("bookmark"),
new String[] { "Select", "Cancel", "Open Item Editor" }, "Select"
);
setWantsInput(true);
setSelectionValues(null); // Would replace with an Object array
setInitialSelectionValue(null);
setComponentOrientation(getRootFrame().getComponentOrientation());
JDialog dialog = createDialog(parent, "Select Item");
selectInitialValue();
dialog.setVisible(true);
dialog.dispose();
Object obj = getInputValue();
if(obj instanceof Item) {
this.openEditor = false;
this.item = (Item) obj;
} else {
this.openEditor = (obj.equals( CANCEL_OPTION));
this.item = null;
}
}
The check for CANCEL_OPTION is not working at all, same with UNDEFINED_OPTION.
Any ideas?
Actually I just had to use the Object returned by the JOptionPane itself: getValue(), problem solved!