How to create a resizable MessageDialog - java

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.

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

Copy and paste MessageDialog message

I am creating a MessageDialog with some information.
MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created.");
I am wanting to be able to copy the number in the dialog so I can paste it somewhere else. Is there a way to set the MessageDialog so I can accomplish this?
The API can be found here. I have not found anything in the API that really helps me out.
No, MessageDialog uses a Label to display the message. In order to allow C&P, you'd need a Text widget instead. So you have to create your own subclass of org.eclipse.jface.dialogs.Dialog.
You may look at the source code of InputDialog as an example. In order to make the text widget read-only, create it with the SWT.READ_ONLY style flag.
You can create a class derived from MessageDialog and override the createMessageArea method with something like:
public class MessageDialogWithCopy extends MessageDialog
{
public MessageDialogWithCopy(Shell parentShell, String dialogTitle, Image dialogTitleImage,
String dialogMessage, int dialogImageType, String [] dialogButtonLabels, int defaultIndex)
{
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType,
dialogButtonLabels, defaultIndex);
}
#Override
protected Control createMessageArea(final Composite composite)
{
Image image = getImage();
if (image != null)
{
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
imageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
}
// Use Text control for message to allow copy
if (message != null)
{
Text msg = new Text(composite, SWT.READ_ONLY | SWT.MULTI);
msg.setText(message);
GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
msg.setLayoutData(data);
}
return composite;
}
public static void openInformation(Shell parent, String title, String message)
{
MessageDialogWithCopy dialog
= new MessageDialogWithCopy(parent, title, null, message, INFORMATION,
new String[] {IDialogConstants.OK_LABEL}, 0);
dialog.open();
}
}
Just use a JTextArea
and then
JTextArea tA= new JTextArea("your message.");
tA.setEditable(true);
then you can add the
MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created.");
after, by changing it a little (you create the JTextArea, and then pass that to the JOptionPane as your message.)

JavaFX default focused button in Alert Dialog

Since jdk 8u40, I'm using the new javafx.scene.control.Alert API to display a confirmation dialog. In the example below, "Yes" button is focused by default instead of "No" button:
public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
final Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
final Optional<ButtonType> result = alert.showAndWait();
return result.get() == ButtonType.YES;
}
And I don't know how to change it.
EDIT :
Here a screenshot of the result where "Yes" button is focused by default :
I am not sure if the following is the way to usually do this, but you could change the default button by looking up the buttons and setting the default-behavior yourself:
public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
final Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
//Deactivate Defaultbehavior for yes-Button:
Button yesButton = (Button) alert.getDialogPane().lookupButton( ButtonType.YES );
yesButton.setDefaultButton( false );
//Activate Defaultbehavior for no-Button:
Button noButton = (Button) alert.getDialogPane().lookupButton( ButtonType.NO );
noButton.setDefaultButton( true );
final Optional<ButtonType> result = alert.showAndWait();
return result.get() == ButtonType.YES;
}
A simple function thanks to crusam:
private static Alert setDefaultButton ( Alert alert, ButtonType defBtn ) {
DialogPane pane = alert.getDialogPane();
for ( ButtonType t : alert.getButtonTypes() )
( (Button) pane.lookupButton(t) ).setDefaultButton( t == defBtn );
return alert;
}
Usage:
final Alert alert = new Alert(
AlertType.CONFIRMATION, "You sure?", ButtonType.YES, ButtonType.NO );
if ( setDefaultButton( alert, ButtonType.NO ).showAndWait()
.orElse( ButtonType.NO ) == ButtonType.YES ) {
// User selected the non-default yes button
}
If you have a look at (private) ButtonBarSkin class, there is a method called doButtonOrderLayout() that performs the layout of the buttons, based in some default OS behavior.
Inside of it, you can read this:
/* now that all buttons have been placed, we need to ensure focus is
set on the correct button. [...] If so, we request focus onto this default
button. */
Since ButtonType.YES is the default button, it will be the one focused.
So #ymene answer is correct: you can change the default behavior and then the one focused will be NO.
Or you can just avoid using that method, by setting BUTTON_ORDER_NONE in the buttonOrderProperty(). Now the first button will have the focus, so you need to place first the NO button.
alert.getButtonTypes().setAll(ButtonType.NO, ButtonType.YES);
ButtonBar buttonBar=(ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);
Note that YES will still have the default behavior: This means NO can be selected with the space bar (focused button), while YES will be selected if you press enter (default button).
Or you can change also the default behavior following #crusam answer.

Can't display intiali value in JoptionPane within JDialog

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

Custom JOptionPane Get "Cancel" action when overriding YES_NO_CANCEL buttons

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!

Categories

Resources