JFrame that pops-up from the taskbar like notifications.

Here i have given the JAVA code to make a popup that happens exactly like the gmail desktop application does.It uses a JFrame to make the window.

Code:

package com.oksbwn.popUp;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.oksbwn.ErrorHandling.handleExceptions;

import resources.RscLoader;

public class popMe extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception{
new popMe("hi", "from Bikash", "ok",15, 125);
}
public popMe(String message,String header,String image, final int time,int height)
{

Runnable r = new newThreadPop(message,header,image,time,height);
new Thread(r).start();
}
}

class newThreadPop implements Runnable {
private String message;
private String header;
private String image;
private int time;
private int height;

public newThreadPop(String message1,String header1,String image1, final int time1,int height1) {
// store parameter for later user
this.message=message1;
this.header=header1;
this.image=image1;
this.time=time1;
this.height=height1;
}

public void run() {
final Runnable runnable = (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.exclamation");
//default
if (runnable != null)
runnable.run();
final JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setType(javax.swing.JFrame.Type.UTILITY);frame.setUndecorated(true);
frame. setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),0));
((JComponent)frame.getContentPane()).setBorder(
BorderFactory.createMatteBorder( 2, 2, 2, 2, Color.black ) );
frame.setSize(300,height);
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.setForeground(Color.red);
ImageIcon headingIcon = new ImageIcon(RscLoader.getImage(image));
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;

final JLabel lblX = new JLabel("X");
lblX.setToolTipText("Closern");
lblX.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
frame.dispose();
}
@Override
public void mouseEntered(MouseEvent e) {
lblX.setForeground(new Color(255, 0, 0));
}
public void mouseExited(MouseEvent e) {
lblX.setForeground(new Color(0, 0, 0));
}
});
lblX.setSize(10,10);
lblX.setFocusable(false);
frame.add(lblX,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(""+message);
messageLabel.setForeground(Color.blue);
frame.add(messageLabel, constraints);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
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());
new Thread(){
@Override
public void run() {
try {
Thread.sleep(time*1000); // time after which pop up will be disappeared.
frame.dispose();
}
catch (Exception e)
{new handleExceptions(e);} };}.start();

}
}


Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WGLabz
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Related posts