Desktop Java

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

  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(){
      @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.

Subscribe
Notify of
guest

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

15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
BoudWijn Van Stenberg
BoudWijn Van Stenberg
11 years ago

it helps ! thanks !

Pawan
Pawan
10 years ago

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

Pawan
Pawan
10 years ago
Reply to  Pawan

Hey Thanks !

Got to know :)

Gs
Gs
10 years ago

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

ARV
ARV
10 years ago
Reply to  Gs

dude, this code is for java desktop applications. You’ll need some other techniques for your requirements

ARV
ARV
10 years ago

this is what i wanted for my project.. Thanks a lot my friend..

Angel
Angel
10 years ago
Vinod
Vinod
10 years ago

Thanks!
really cool!

Sameera
Sameera
10 years ago

Thank you very much fellow.it was make me awesome B|

sumit
sumit
10 years ago

please share java file …

Janet
Janet
9 years ago

How to make the notification appear after 3 hours?

Malleswari
Malleswari
8 years ago

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

Deepali
Deepali
7 years ago

Hey ,

I want exactly like same i tried the same code, but seems not happening .

please help

sameera chathuranga
sameera chathuranga
6 years ago

Create new message notification pop up in Javafx

Flourish Sam
Flourish Sam
5 years ago

Pls how can I include the pop up I have created with your sample to function on my web page?

Back to top button