Create new message notification pop up in Java.

This article is about “How to create new message notification pop up in java?


Pop up example
This image is taken from internet.

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:

First look of pop up.

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:

Second look of pop up.

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:

Third look of pop up.

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. Thanks and Regards.


33 thoughts on “Create new message notification pop up in Java.

  1. Pingback: JavaPins
  2. hey…this really helped…thanks..
    One problem
    When i made it disappear,then the notification disappear…but with in that time if one more notification comes,then it come and disappear,but the previous notification does not disappear.
    It gets stuck…Please help…

    1. Hi karthik,

      Some more information is required to help you:
      How you make it disappear? Do you use same code or modified it to fit your needs?
      Does it get closed after clicking on closeButton?

      Also make sure that you are calling dispose method of correct popup frame and you are not mixing frame variables of all opened popup frames.

  3. Thanks for your reply.
    Now it is working fine.I made frame as instance variable and made modification in the same frame when new notification comes.

  4. Thanks for the tutorial! It’s really helpful! What if there is 2 pop ups at the same time? How can I display the 2nd pop up on top of the first one? Thanks!

    1. Hi Lily,

      In your situation what you can do is maintain a list of popups. Whenever you need to display a popup add it in this list. Now whenever new popup comes check this list’s size and if its greater than one then update popup’s location based on list’s size. Instead of list you can also simply use a integer counter as you just need to know currently shown popup count.

      In this line you can change the location:

      frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight());

      instead of this you can have:

      frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - (frame.getHeight() * (n+1)));

      Here ‘n’ is the number of popup opened before this popup. Let me know if any further explanation is required. 🙂

      1. Thanks for the reply Harsh, it’s popping up now! 🙂 However, the cloesButton and sleep after 5 secs only works on one frame. What can I do to enable then on the other frames too? Thanks again! 🙂

        1. Hi Lily,

          If you have separate instance of all popup then it should work. Make sure you are not mixing up frame variables of popup. Don’t take only 1 frame variable to handle all popup. You should have different var for all. For this maintaining list of popup will be better option.

    1. Hi ,

      Yes there are multiple imports. Which kind of errors are you getting? Let me know the errors and I will try to solve them for you.

      Thanks & regards,
      Harsh Raval.

  5. Hi! How do I create arrow keys to click and see the prev or next messages on the pop-up?

      1. Hi,

        This really helped…thanks
        One Problem i’m having is, when we set number of popup using this line

        frame.setLocation(scrSize.width – frame.getWidth(), scrSize.height – toolHeight.bottom – (frame.getHeight() * (n+1)));

        It works as expected but when i have 10 popups, i’m able to see 7 only and the remaining are hidden above.

        1. How can bring down the remaining popups(8,9,10) to the bottom.
        2. I tried setting timer for the popups but not getting idea for bringing down the remaining ones.

        How do i do this?


        1. Try to keep track of all and rearrange position of all once a popup is close, bring the popups, above the closing popup, down by height of the closing popup. This way all be on screen at some point of time. Also make sure you close them in same sequence as they appeared so that there is at least some time when you can see the last popup and read information in it.


  6. First thank you for this tutorial! it is great, i was wondering if you know how to make the JFrame to fade in and out? Thank you again.

  7. Here i have 10 pop up.but here i am able to see only one popup.if i close one popup next one,i can able to see.But here i want to see all popup at a time like poping pop one above one..And from this blog i have mention

    Here int n =0; i have taken

    frame.setLocation(scrSize.width – frame.getWidth(), scrSize.height – toolHeight.bottom – (frame.getHeight() * (n+1)));
    But still i am not able to see one above one.Can You suggest Me?

  8. Yeah it is showing after increment n value..:P…
    But not showing in proper manner like i am getting all data from feed URL.In this URL how many entries are there i dont know.suppose 10 entries are there ,so 10 pop ups are showing,if i close one pop up,2nd popup is not placing in 1st pop up how to minimize this one .Please help me out

    1. Hi Abhishek,

      You have to code for this in close method of the popup. First you have to maintain array or map of open popups. Then on close of any popup repostion everyother popup in array or map by calculating their new location based on which popup is closed. You simply have to decrease ‘n’ in location calculation by 1 for popups that are on top of closing popup.

      Rajshekar was facing same and I replied similar to him as well, see comments above.


      1. would u please help me by providing some example. actually i dont know how to replace 2nd popup in 1st popup after some specific time.

  9. Hi Harry,
    Here i am able to show all popups in one over one.After that i want to show in Another frame,where i can scroll the that Frame. In this frame my popups frames will be can you Please tell how to do.And that popups are one one Frame.

  10. hi ….. i am able to show popup one after one. but problem is that after dispose one frame after a specific time 2nd popup does not replace in 1st popup position. please provide some example for it.

      1. thanks for ur reply.its OK, just tell me how to maintain an array or map of open popup.On close of any popup how to re-position every-other popup in array or map by calculating their new location based on which popup is closed. i did the same u told before for showing more than one popup. but actually i cant replace popup after closing another popup.

  11. I need help making notification messages, can you help? it’s very similar code and it does the same thing.

Leave a Reply to Lily Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.