jnotify is an API for creating notifications in Java applications, designed to provide informative messages.
Step 1: To use jnotify, include the library from https://sourceforge.net/projects/jnotify-api/ in your project.
Step 2: You can use any constructor according to your needs. For example:
new NotifyWindow(NotifyType.DEFAULT_NOTIFICATION, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", NotifyWindow.NORMAL_DELAY, NPosition.CENTER);Constructor Parameters:
NotifyType: Specifies the type of notification. Available types include:NotifyType.DEFAULT_NOTIFICATIONNotifyType.SUCCESS_NOTIFICATIONNotifyType.ERROR_NOTIFICATIONNotifyType.WARNING_NOTIFICATION
String message: The message to be displayed in the notification.int delay: The duration for which the notification will be displayed. Available options:NotifyWindow.NORMAL_DELAYNotifyWindow.SHORT_DELAYNotifyWindow.LONG_DELAY
NPosition: The position on the screen where the notification will appear. Available positions:NPosition.CENTERNPosition.TOP_LEFTNPosition.TOP_RIGHTNPosition.BOTTOM_LEFTNPosition.BOTTOM_RIGHT
NotifyWindow: The main class for creating and displaying notifications.NotifyType: A class defining the visual style and content of a notification.NPosition: An enum defining the possible positions for the notification window on the screen.ScreenDimension: An interface defining constants and methods related to screen dimensions.
src/com/sbix/jnotify/NPosition.java
Defines the possible positions for the notification window on the screen. It also provides methods to calculate the exact coordinates for each position.
Enum Constants:
TOP_LEFT: Represents the top-left corner of the screen.getPosition(JDialog window): Calculates the position for the notification window at the top-left corner.
TOP_RIGHT: Represents the top-right corner of the screen.getPosition(JDialog window): Calculates the position for the notification window at the top-right corner.
BOTTOM_LEFT: Represents the bottom-left corner of the screen.getPosition(JDialog window): Calculates the position for the notification window at the bottom-left corner.
BOTTOM_RIGHT: Represents the bottom-right corner of the screen.getPosition(JDialog window): Calculates the position for the notification window at the bottom-right corner.
CENTER: Represents the center of the screen.getPosition(JDialog window): Calculates the position for the notification window at the center of the screen.
src/com/sbix/jnotify/NotifyType.java
Represents the visual style and content of a notification. This class is responsible for displaying the notification message with a specific background color based on the notification type.
Static Color Constants:
DEFAULT_NOTIFICATION:new Color(0x7f8c8d)- Represents a default notification type with a neutral color.
SUCCESS_NOTIFICATION:new Color(0x2ecc71)- Represents a success notification type, typically green.
ERROR_NOTIFICATION:new Color(0xc0392b)- Represents an error notification type, typically red.
WARNING_NOTIFICATION:new Color(0xf39c12)- Represents a warning notification type, typically orange or yellow.
Constructor:
NotifyType(Color notficationType, String message)- Constructs a new
NotifyTypepanel. - Parameters:
notficationType: The backgroundColorfor the notification, typically one of the predefined staticColorconstants.message: The message text to be displayed in the notification. HTML formatting can be used.
- Constructs a new
Public Methods:
static Insets getScreenInsets()- Calculates the insets of the screen, which can be used to determine the usable area of the screen, excluding elements like the taskbar.
- Returns: An
Insetsobject representing the top, left, bottom, and right insets of the screen.
src/com/sbix/jnotify/ScreenDimension.java
Interface defining constants and methods related to screen dimensions and notification positioning. This interface provides a centralized way to access screen properties and calculate notification positions.
Constants:
Dimension screenSize: The overall dimensions (width and height) of the primary screen.int heightScreen: The height of the primary screen in pixels.int widthScreen: The width of the primary screen in pixels.int blocSpacing: The spacing in pixels (default: 4) between multiple notifications when they are displayed simultaneously in the same corner of the screen.Insets screenInsets: Insets of the screen, representing the space occupied by elements like the taskbar. Retrieved usingNotifyType.getScreenInsets().Point topRightInsets:Pointrepresenting the insets for the top-right corner (right inset, top inset).Point topLeftInsets:Pointrepresenting the insets for the top-left corner (left inset, top inset).Point bottomRightInsets:Pointrepresenting the insets for the bottom-right corner (right inset, bottom inset).Point bottomLeftInsets:Pointrepresenting the insets for the bottom-left corner (left inset, bottom inset).
Methods:
Point getPosition(JDialog window)- Calculates the position (x, y coordinates) for a notification window. Implementations of this method (e.g., in
NPosition) define the specific logic for positioning. - Parameters:
window: The notification window (JDialog) for which to calculate the position.
- Returns: A
Pointobject representing the calculated (x, y) coordinates.
- Calculates the position (x, y coordinates) for a notification window. Implementations of this method (e.g., in
src/com/sbix/jnotify/NotifyWindow.java
Represents a notification window that can be displayed on the screen. This window shows a message with a specific style (type) and can be positioned at various locations on the screen. It automatically disappears after a configurable delay. The window also responds to mouse events to pause the disappearance timer when hovered.
Constructors:
NotifyWindow(Color notification, String message, int delay, NPosition position)- Constructs a new notification window with specified type, message, delay, and position.
- Parameters:
notification: TheColorrepresenting the type of notification (e.g.,NotifyType.SUCCESS_NOTIFICATION).message: The message to be displayed.delay: The duration in milliseconds for visibility (e.g.,NotifyWindow.NORMAL_DELAY).position: TheNPositionon the screen.
NotifyWindow(Color notification, String message, int delay)- Constructs a new notification window with specified type, message, and delay, using the default position (
NPosition.CENTER).
- Constructs a new notification window with specified type, message, and delay, using the default position (
NotifyWindow(Color notification, String message)- Constructs a new notification window with specified type and message, using default delay (
NotifyWindow.NORMAL_DELAY) and position (NPosition.CENTER).
- Constructs a new notification window with specified type and message, using default delay (
NotifyWindow(String message)- Constructs a new notification window with a specified message, using default type (
NotifyType.DEFAULT_NOTIFICATION), delay (NotifyWindow.NORMAL_DELAY), and position (NPosition.CENTER).
- Constructs a new notification window with a specified message, using default type (
NotifyWindow(String message, NPosition position)- Constructs a new notification window with a specified message and position, using default type (
NotifyType.DEFAULT_NOTIFICATION) and delay (NotifyWindow.NORMAL_DELAY).
- Constructs a new notification window with a specified message and position, using default type (
Public Static Final Delay Constants:
int SHORT_DELAY = 1500: Short delay for notification visibility (1500 milliseconds).int NORMAL_DELAY = 2500: Normal delay for notification visibility (2500 milliseconds).int LONG_DELAY = 4000: Long delay for notification visibility (4000 milliseconds).
Public Methods:
NPosition getPosition()- Gets the current screen position of the notification window.
- Returns: The
NPositionenum constant representing the current position.
void setPosition(NPosition position)- Sets the screen position for the notification window. (Note: Best used before window is visible).
- Parameters:
position- TheNPositionenum constant.
void clear(NPosition position)- Adjusts static inset values when a notification at a specific corner is closed, to allow correct stacking of subsequent notifications.
- Parameters:
position- TheNPositionof the closed window.
void actionPerformed(ActionEvent e)- Handles timer events for showing and fading out the notification.
- Parameters:
e- TheActionEvent.
void mouseClicked(MouseEvent e)- Invoked when the mouse is clicked on the notification. (No specific action implemented).
- Parameters:
e- TheMouseEvent.
void mouseEntered(MouseEvent e)- Invoked when the mouse enters the notification area; pauses the fade-out timer.
- Parameters:
e- TheMouseEvent.
void mouseExited(MouseEvent e)- Invoked when the mouse exits the notification area. (No specific action implemented).
- Parameters:
e- TheMouseEvent.
void mousePressed(MouseEvent e)- Invoked when a mouse button is pressed on the notification. (No specific action implemented).
- Parameters:
e- TheMouseEvent.
void mouseReleased(MouseEvent e)- Invoked when a mouse button is released on the notification. (No specific action implemented).
- Parameters:
e- TheMouseEvent.
Point getPosition(JDialog window)- Gets the current location of this notification window (implements
ScreenDimension). - Parameters:
window- TheJDialogwindow (expected to be this instance). - Returns: A
Pointobject with current (x,y) coordinates.
- Gets the current location of this notification window (implements
Mohsine AMECHTAK
