Create new message notification pop up in Java
A sample code is given below:
String message = 'You got a new notification message. Isn't it awesome to have such a notification message.'; String header = 'This is header of notification message'; JFrame frame = new JFrame(); frame.setSize(300,125); frame.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.weightx = 1.0f; constraints.weighty = 1.0f; constraints.insets = new Insets(5, 5, 5, 5); constraints.fill = GridBagConstraints.BOTH; JLabel headingLabel = new JLabel(header); headingLabel .setIcon(headingIcon); // --- use image icon you want to be as heading image. headingLabel.setOpaque(false); frame.add(headingLabel, constraints); constraints.gridx++; constraints.weightx = 0f; constraints.weighty = 0f; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTH; JButton cloesButton = new JButton('X'); cloesButton.setMargin(new Insets(1, 4, 1, 4)); cloesButton.setFocusable(false); frame.add(cloesButton, constraints); constraints.gridx = 0; constraints.gridy++; constraints.weightx = 1.0f; constraints.weighty = 1.0f; constraints.insets = new Insets(5, 5, 5, 5); constraints.fill = GridBagConstraints.BOTH; JLabel messageLabel = new JLabel('<HtMl>'+message); frame.add(messageLabel, constraints); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true);
Output of this will be:
Here I have created a JFrame and add two labels; first is headingLabel which is header label and second is messageLabel which will contains message information; and a close button. I have used GridBagLayout but you can use any of your choice.
Now to make this frame look like a popup we have to remove the title bar and border from this frame. For this add following line after frame.setSize(…); :
frame.setUndecorated(true);
Now the output will be:
Note that now our frame cannot be closed as it do not have title bar close button. So to make our close button to work as frame closing button change its declaration as follows:
JButton cloesButton = new JButton(new AbstractAction('x') { @Override public void actionPerformed(final ActionEvent e) { frame.dispose(); } });
After adding it you will get error as “Cannot refer to a non-final variable frame inside an inner class defined in a different method”. To get rid of this error you can adopt one of following solution:
- Make your frame variable as final.
- Make your frame variable a global variable in class.
- Make your class extends JFrame and remove frame variable at all.
Now when you run your program it will look like same as in figure 2 but now you will be able to close your frame by clicking on closeButton.
You will notice that your frame appears at the top of screen so change to its location bottom right corner of the screen add following lines after creating frame:
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());// height of the task bar frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight());
Now when you run it will look like this:
Now to make it to disappear after predefined time add following lines at the end:
new Thread(){ @Override public void run() { try { Thread.sleep(5000); // time after which pop up will be disappeared. frame.dispose(); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start();
Up to this you have successfully created a notification pop up that will appear at bottom-right corner of screen and disappear after some time if close button is not clicked.
So as a final touch you can design it as you want by applying look and feel or by applying different colours in frame.
Also you can make it appear on top of all windows by adding:
frame.setAlwaysOnTop(true);
Some things to notice in above code blocks:
1. <HtMl> tag in messageLabel. It is to make word wrap in label. But make sure you text in message does not exceed some specific amount of length. You can adjust this and height of pop up as per your need.
2. “headingIcon” is not declared in code. It is the image icon you want to use instead of devil icon in screen shot as a heading title icon. A sample declaration will look like:
ImageIcon headingIcon = new ImageIcon(“image_url”);
3. Currently a new window for our pop up is shown in task bar so if you want that no window is shown for pop up in task bar change JFrame to JDialog.
4. In above code default timeout before the pop up will be disappear is taken as 5 seconds you can update it as per your need by editing following line in code:
Thread.sleep(5000); // time after which pop up will be disappeared.
5. To make close button look like default title bar’s close button “x” is taken in his text. You can write it close if you want to.
Hope this helps you.
Happy coding and don’t forget to share!
Reference: Create new message notification pop up in Java. from our JCG partner Harsh Raval at the harryjoy blog.
it helps ! thanks !
Hi!
Thanks a lot for the coode which i was looking a lot !
Can u tell me abt Insets? Is that a method or wat?
Advance Thanks ! :)
Hey Thanks !
Got to know :)
Hi..
How can I add this code to my website? Like if I add something new it should appear like ur pop up.. My codings r HTML, JAVASCRIPT.. Is it possible me to add this? Pls help me with ths..
Advance thanks :)
dude, this code is for java desktop applications. You’ll need some other techniques for your requirements
this is what i wanted for my project.. Thanks a lot my friend..
Hi! Can I know how to include arrows keys to show more messages in the notification? like this pic u show in here: http://1-ps.googleusercontent.com/x/www.javacodegeeks.com/1.bp.blogspot.com/-epIAcV1yLtE/UHxwsqIyayI/AAAAAAAADdU/7W6uYldK0po/s1600/xtest+,281,29.png.pagespeed.ic.9x7n_yVv1C.png
Thanks!
really cool!
Thank you very much fellow.it was make me awesome B|
please share java file …
How to make the notification appear after 3 hours?
if this popup displays on any other UI components(displays over these components ) like scroll table,buttons ,or checkbox labels radiao buttons then this pop is blinking, How can we solve this issue
Hey ,
I want exactly like same i tried the same code, but seems not happening .
please help
Create new message notification pop up in Javafx
Pls how can I include the pop up I have created with your sample to function on my web page?