Home » Java » Desktop Java » Create new message notification pop up in Java

About Harsh Raval

Harsh Raval

Create new message notification pop up in Java

First create JFrame to work as pop up. Add some JLabels in it to contain information and assign them at proper location to look like a notification message.

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.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.
frame.add(headingLabel, constraints);
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));
frame.add(cloesButton, constraints);
constraints.gridx = 0;
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);

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(…); :

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') {
        public void actionPerformed(final ActionEvent e) {

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:

  1. Make your frame variable as final.
  2. Make your frame variable a global variable in class.
  3. 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(){
      public void run() {
           try {
                  Thread.sleep(5000); // time after which pop up will be disappeared.
           } catch (InterruptedException e) {

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:


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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

13 Comments on "Create new message notification pop up in Java"

Notify of
Sort by:   newest | oldest | most voted
BoudWijn Van Stenberg
BoudWijn Van Stenberg

it helps ! thanks !



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 :)



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..


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