Monday, June 27, 2016

DESKTOP NOTIFICATION IN JAVA (SWING)

DESKTOP NOTIFICATION IN JAVA (SWING)



import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;


public class Notification
{
    public static void init()
    {
                String message ="ALL POWER IS WITH IN YOU ,YOU CAN DO ANYTHING AND EVERYTHING BELIVE IN THAT";
                String header = "NOTIFICATION MESSAGE";
                final JFrame frame = new JFrame();
                frame.setSize(300,125);
                frame.setUndecorated(true);
                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);
                ImageIcon img=new ImageIcon("question_icon1.png");
                headingLabel .setIcon(img); // --- 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(new AbstractAction() {
              
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        frame.dispose();
                      
                    }
            });
                cloesButton.setText("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);
                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());
                frame.setVisible(true);
    }
    public static void main(String args[])
    {
        init();
      
    }

}

No comments:

Post a Comment