Can't display intiali value in JoptionPane within JDialog - java

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

Related

Show JOptionPane (with dropdown menu) on the top of other windows

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;
}
}

How to create a resizable MessageDialog

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.

JPopupMenu auto hide on JoptionPane Confirmdialog

I know that in order to prevent JOptionpane from hiding behind any of the frame we have to give the current frame as parent frame to JOptionpane.
I have a JTree with popupmenu
it has popup menu follows
Add
Rename
Delete
when I click the delete menu i'll call the showDeleteConfirmation() to confirm the action to delete or not
But the problem if I use currentMainframe(the one which jtree is present) as parent frame for JOptionpane and when I click the JPopumenu is not hiding(still in focus) so I have to click on Joptionpane once (to hide the popupmenu) and then only I can select the options
If I use null as parentframe it is working perfectly(onclicking the the menuitem it is automatically hiding).
How to solve the issue
//Have to click anywhere on JOptionpane to gain focus(also to hide popupmenu)
public static Boolean showDeleteConfirmation() {
if (deleteConfirmation) {
int value = JOptionPane.showConfirmDialog(currentMainFrame, "Are you sure want to delete?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
return value == JOptionPane.YES_OPTION;
}
return true;
}
//This is working perfectly
public static Boolean showDeleteConfirmation() {
if (deleteConfirmation) {
int value = JOptionPane.showConfirmDialog(null, "Are you sure want to delete?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
return value == JOptionPane.YES_OPTION;
}
return true;
}
I manually called JPopupmenu.hide() before calling that function.It solved the problem

JOptionPane title bar icon

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;
}

Automatic toggling of character width by Windows 7 input methods in Java

I have a couple of input methods for writing (Traditional Chinese) Taiwanese that come with the Windows 7. Also, all of the input methods have an option to switch the character width (single byte/double byte characters).
Chinese (Traditional) - New Quick
Chinese (Traditional) - ChangJie
Chinese (Traditional) - Quick
Chinese (Traditional) - Phonetic
Chinese (Traditional) - New Phonetic
Chinese (Traditional) - New ChangJie
If I select one of these input methods in Java application and set the character width to half-width(single byte character mode) i can successfully input text in JTextField. But, if the application displays some dialog box (e.g. JOptionPane) or pop up window, the input method character width will automatically change to full-width(double byte character mode). After that, the user must manually toggle to half-width characters.
I can programmatically switch on or off the input method using the Java class "InputContext", but i can't control if the input method is set to full-width/half-width (single/double byte) character mode.
I thought maybe it could be disabled from the Windows input method settings, but there was no option related to automatic switching of the character width.
The question is: Is there a way to handle (disable) this automatic toggling ?
Here is an example code to test this with the above input methods:
public class Example implements ActionListener {
JFrame f = new JFrame("pasod");
JTextField txt = new JTextField();
Button btn = new Button("Locale");
public Example() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
btn.addActionListener(this);
panel.add(btn);
panel.add(txt);
f.add(panel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(800, 100);
f.setVisible(true);
}
public static void main(String[] args) {
new Example();
}
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(btn, "Neso", "Neso",
JOptionPane.INFORMATION_MESSAGE);
}
}
Thanks.
Ok I've had a trace through the Java source code looking for anything that stands out;
You call JOptionPane.showMessageDialog() this overloads to JOptionPane.showOptionDialog();
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);
pane.setComponentOrientation(((parentComponent == null) ?
getRootFrame() : parentComponent).getComponentOrientation());
int style = styleFromMessageType(messageType);
JDialog dialog = pane.createDialog(parentComponent, title, style);
pane.selectInitialValue();
dialog.show();
//..Result handling code
}
So we look into createDialog();
public JDialog createDialog(String title) throws HeadlessException {
int style = styleFromMessageType(getMessageType());
JDialog dialog = new JDialog((Dialog) null, title, true);
initDialog(dialog, style, null);
return dialog;
}
So we check the constructor/s of JDialog these all call dialogInit();
protected void dialogInit() {
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
setLocale( JComponent.getDefaultLocale() );
setRootPane(createRootPane());
setRootPaneCheckingEnabled(true);
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
}
}
sun.awt.SunToolkit.checkAndSetPolicy(this, true);
}
Here we've found setLocale( JComponent.getDefaultLocale() );;
So it appears whenever you create a JDialog, whether it be indirect or not the locale of your program is reset to default, I'm guessing this includes resetting your input settings.
There are a few ways you can set the default Locale (programatically, system properties or runtime args); Details found here
Hope that helps you
I did a simple test:
I opened IE, selected a tab, and at the address bar, set Chinese IME to be half width. Then click another tab, the IME change to full width automatically.
So I don't think it had anything to do with Java. It's a Windows behavior.

Categories

Resources