How to Use NetBeans Platform Notifications
Join the DZone community and get the full member experience.
Join For FreeOne thing I like a lot are the way notifications are handled when you're creating applications on the NetBeans Platform. If you have been using NetBeans IDE, you should see that if you load something a text appears in the status area, i.e., the bottom left corner.
To use this notification area you need to simply use this line in your own application:
StatusDisplayer.getDefault().setStatusText("Hola mundo");
There is a notification balloon too, the one that you should have seen when NetBeans IDE finds updates. That balloon is available for your application too! How to use it? Simple… in Quintin Beuke's blog, there are three classes that the author nicely shares with everyone on the net, which I'm using in my application with some little modifications.
These are the classes:
package org.metalklesk.utilities;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.openide.NotifyDescriptor;
/**
*
* @author qbeukes.blogspot.com, used by metalklesk
*/
public enum MessageType {
PLAIN (NotifyDescriptor.PLAIN_MESSAGE, null),
INFO (NotifyDescriptor.INFORMATION_MESSAGE, "info.png"),
QUESTION(NotifyDescriptor.QUESTION_MESSAGE, "question.png"),
ERROR (NotifyDescriptor.ERROR_MESSAGE, "error.png"),
WARNING (NotifyDescriptor.WARNING_MESSAGE, "warning.png");
private int notifyDescriptorType;
private Icon icon;
private MessageType(int notifyDescriptorType, String resourceName) {
this.notifyDescriptorType = notifyDescriptorType;
if (resourceName == null) {
icon = new ImageIcon();
} else {
icon = loadIcon(resourceName);
}
}
private static Icon loadIcon(String resourceName) {
URL resource = MessageType.class.getResource("images/" + resourceName);
System.out.println(resource);
if (resource == null) {
return new ImageIcon();
}
return new ImageIcon(resource);
}
int getNotifyDescriptorType() {
return notifyDescriptorType;
}
Icon getIcon() {
return icon;
}
}
Don't forget to place the images into yourpackage/images folder.
package org.metalklesk.utilities;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
/**
*
* @author qbeukes.blogspot.com, used by metalklesk
*/
public class MessageUtil {
private MessageUtil() {}
/**
* @return The dialog displayer used to show message boxes
*/
public static DialogDisplayer getDialogDisplayer() {
return DialogDisplayer.getDefault();
}
/**
* Show a message of the specified type
*
* @param message
* @param messageType As in {@link NotifyDescription} message type constants.
*/
public static void show(String message, MessageType messageType) {
getDialogDisplayer().notify(new NotifyDescriptor.Message(message,
messageType.getNotifyDescriptorType()));
}
/**
* Show an exception message dialog
*
* @param message
* @param exception
*/
public static void showException(String message, Throwable exception) {
getDialogDisplayer().notify(new NotifyDescriptor.Exception(exception, message));
}
/**
* Show an information dialog
* @param message
*/
public static void info(String message) {
show(message, MessageType.INFO);
}
/**
* Show an error dialog
* @param message
*/
public static void error(String message) {
show(message, MessageType.ERROR);
}
/**
* Show an error dialog for an exception
* @param message
* @param exception
*/
public static void error(String message, Throwable exception) {
showException(message, exception);
}
/**
* Show an question dialog
* @param message
*/
public static void question(String message) {
show(message, MessageType.QUESTION);
}
/**
* Show an warning dialog
* @param message
*/
public static void warn(String message) {
show(message, MessageType.WARNING);
}
/**
* Show an plain dialog
* @param message
*/
public static void plain(String message) {
show(message, MessageType.PLAIN);
}
}
package org.metalklesk.utilities;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.ErrorManager;
import org.openide.awt.Notification;
import org.openide.awt.NotificationDisplayer;
/**
*
* @author qbeukes.blogspot.com, used by metalklesk
*/
public class NotifyUtil {
private NotifyUtil() {}
/**
* Show message with the specified type and action listener
*/
public static void show(String title, String message, MessageType type, ActionListener actionListener, boolean clear) {
Notification n = (Notification) NotificationDisplayer.getDefault().notify(title, type.getIcon(), message, actionListener);
if(clear == true)
n.clear();
}
/**
* Show message with the specified type and a default action which displays the
* message using {@link MessageUtil} with the same message type
*/
public static void show(String title, final String message, final MessageType type, boolean clear) {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MessageUtil.show(message, type);
}
};
show(title, message, type, actionListener, clear);
}
/**
* Show an information notification
* @param message
*/
public static void info(String title, String message, boolean clear) {
show(title, message, MessageType.INFO, clear);
}
/**
* Show an error notification
* @param message
*/
public static void error(String title, String message, boolean clear) {
show(title, message, MessageType.ERROR, clear);
}
/**
* Show an error notification for an exception
* @param message
* @param exception
*/
public static void error(String title, final String message, final Throwable exception , boolean clear) {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ErrorManager.getDefault ().notify (exception);
//MessageUtil.showException(message, exception);
}
};
show(title, message, MessageType.ERROR, actionListener, clear);
}
/**
* Show an warning notification
* @param message
*/
public static void warn(String title, String message, boolean clear) {
show(title, message, MessageType.WARNING, clear);
}
/**
* Show an plain notification
* @param message
*/
public static void plain(String title, String message, boolean clear) {
show(title, message, MessageType.PLAIN, clear);
}
}
With these 3 classes in your NetBeans Platform application, you can use the balloon notification like this:
NotifyUtil.warn("title", "warning message", false);
NotifyUtil.show("title", "info message", false);
NotifyUtil.show("title", "info message", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//do something
}
};, false);
NotifyUtil.error("title", "error message”, false);
NotifyUtil.error("title", "error message", exception, false);
The last one shows our exception in an info dialog.
And, finally, if we want to show a notification in an info dialog, instead of using JOptionPanel, we can use this:
MessageUtil.showException("message", exception);
MessageUtil.error("message", exception);
MessageUtil.error("message");
MessageUtil.info("message");
MessageUtil.question("message");
MessageUtil.plain("message");
And that's it!
Here are some screenshots where you can see the notifications in action:
Opinions expressed by DZone contributors are their own.
Trending
-
Observability Architecture: Financial Payments Introduction
-
Execution Type Models in Node.js
-
Reducing Network Latency and Improving Read Performance With CockroachDB and PolyScale.ai
-
Building and Deploying Microservices With Spring Boot and Docker
Comments