Anarkie
Anarkie

Reputation: 695

Simple SWT Alert Message with custom icon?

Im new to SWT, making a GUI and I just want to give an alert message after the user clicking "OK" button. Like "Your parameters are wrong" with an error icon preferably and "Your parameters are correct" with a check icon preferably. I did a little bit searching and it can be possible with MessageBox() but it doesnt have check or tick icon, I also don't want to bother creating a custom image and then applying it to the dialog. With MessageBox() I have to create a new MessageBox object, set the text and then open the text... Isn't there a simple, one line solution for this like in Swing? StaticClass.showmsg(type,"String"); for example? If possible I would like to avoid JFace because thats another library and I got tons of errors with JFace, can't be that hard... This is also what I got with MessageBox(). Doesn't look ethic.

Message box example

Edit: I added jface and jface also doesnt have a check icon(MessageDialog) so looks like there is no other chance I will have to add the check icon on my own but how? My current warning message:

MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", "Input parameters are wrong!")

I wished there was a MessageDialog.openOK(), but there isnt so how can I insert my own icon into these messages?

Upvotes: 2

Views: 2820

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

The SWT MessageBox uses the native message dialog of the OS/window system (at least on Windows). There is no predefined check image for the MessageBox in SWT.

For an informational message like 'Your parameters are ok', SWT.ICON_INFORMATION is provided and ICON_ERROR for - well - error messages.

If that doesn't suit your needs you can either reach out to JFace or write your own message dialog in pure SWT. If you opt for JFace, use the MessageDialog for standard dialogs like error, info, warning, etc. or use the IconAndMessageDialog if you need more control over the dialog contents.

Upvotes: 2

IdusOrtus
IdusOrtus

Reputation: 1116

I'm uncertain how you would go about finding the position of the console window (which you would need in order to align a pane to it) but if you're concerned about the overall appearance of your application you're likely best off using only one or the other to maintain consistency.

The following may be of use if you opt for a GUI.

If the data you are collecting is not simple you might want to take a look at NetBeans Swing GUI Builder (Matisse) . Folks have various opinions on builders but I feel the NetBeans one is a fair way to become familiar with Swing and you can't beat the price. Take an hour or so, walk through the basic tutorials, and you'll be designing simple (albeit vanilla) GUIs in no time.

If the data you are collecting is simple you could use showInputDialog(). Here's a simple, far from perfect, drop-down example. If you provide more information regarding the data you are collecting I can provide a different example.

String response;
String[] possibleValues = { "a Pony", "a Job", "World Peace"};

JOptionPane.showInputDialog(null,
        "Choose one", "Select Something",
        JOptionPane.INFORMATION_MESSAGE, null,
        possibleValues, possibleValues[0]);

response = String.format("You desire %s\n", possibleValues);
JOptionPane.showMessageDialog(null, response);

Simple examples of JOptionPane message windows, with custom icons from URL:

public void foo() throws MalformedURLException
{

    ImageIcon failIcon = new ImageIcon(new URL("https://cdn0.iconfinder" +
            ".com/data/icons/star-wars/512/darth_vader-32.png"));
    ImageIcon okIcon = new ImageIcon(new URL("http://www.fimfiction-static" +
            ".net/images/avatars/98207_32.jpg?1373627869"));
    JOptionPane.showMessageDialog(null, "Parameters are incorrect.", "Derp",
            JOptionPane.ERROR_MESSAGE, failIcon);
    JOptionPane.showMessageDialog(null, "Parameters are correct.", "Rejoice",
            JOptionPane.PLAIN_MESSAGE, okIcon );

Good luck!

Upvotes: 0

Related Questions