How to create system notification popup like outlook using Swing in java
It would be very easy to make system notification pop up like Microsoft outlook in swing.
I have created following two java classes to make system notification works. First class handled the basic and core level implementation like creating a form and making AWT api to work. Second class is used for testing purpose.1. NotificationPopup.java
package com.vsquaresolution.system.nofitifcation; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.LinearGradientPaint; import java.awt.Toolkit; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class NotificationPopup extends JDialog { private final LinearGradientPaint lpg; public NotificationPopup() { setUndecorated(true); setSize(300, 50); // size of the screen final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // height of the task bar final Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration()); final int taskBarSize = scnMax.bottom; setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight()); // background paint lpg = new LinearGradientPaint(0, 0, 0, getHeight() / 2, new float[] { 0f, 0.3f, 1f }, new Color[] { new Color(1f, 1f, 1f), new Color(1f, 1f, 1f), new Color(1f, 1f, 1f) }); // blue background panel setContentPane(new BackgroundPanel()); } private class BackgroundPanel extends JPanel { public BackgroundPanel() { setOpaque(true); } @Override protected void paintComponent(final Graphics g) { final Graphics2D g2d = (Graphics2D) g; // background g2d.setPaint(lpg); g2d.fillRect(1, 1, getWidth() - 2, getHeight() - 2); g2d.setColor(Color.ORANGE); //border color // border g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1); } } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (final Exception e1) { e1.printStackTrace(); } final NotificationPopup f = new NotificationPopup(); final Container c = f.getContentPane(); c.setLayout(new GridBagLayout()); final 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; final JLabel l = new JLabel("You have got 2 new Messages."); l.setOpaque(false); c.add(l, constraints); constraints.gridx++; constraints.weightx = 0f; constraints.weighty = 0f; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTH; // final JButton b = new JButton(new AbstractAction("x") { // // @Override // public void actionPerformed(final ActionEvent e) { // f.dispose(); // } // }); // b.setOpaque(false); // b.setMargin(new Insets(1, 4, 1, 4)); // b.setFocusable(false); // // c.add(b, constraints); f.setVisible(true); } }); } }2. TestNotificationPopUp.java
package com.vsquaresolution.system.nofitifcation; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class TestNotificationPopUp extends javax.swing.JFrame { public TestNotificationPopUp() { initComponents(); } ////GEN-BEGIN:initComponents private void initComponents() { jPanel2 = new javax.swing.JPanel(); jButton5 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Antenna"); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(" System ")); jButton5.setText("Open Notification"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup() .add(182, 182, 182) .add(jButton5) .add(256, 256, 256)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(28, 28, 28) .add(jButton5) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(29, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (final Exception e1) { e1.printStackTrace(); } final NotificationPopup f = new NotificationPopup(); final Container c = f.getContentPane(); c.setLayout(new GridBagLayout()); final 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; final JLabel l = new JLabel("You have got 2 new Messages."); l.setOpaque(false); c.add(l, constraints); constraints.gridx++; constraints.weightx = 0f; constraints.weighty = 0f; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTH; f.setVisible(true); } }); }//GEN-LAST:event_jButton5ActionPerformed public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TestNotificationPopUp().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton5; private javax.swing.JPanel jPanel2; // End of variables declaration//GEN-END:variables }
3 comments:
Interesting blog attracted me.
I hope you will post more Updates. Thanks for the post.
pop up system
I am glad to find amazing information from the blog.
Thanks for sharing the information.
Pop Up System
Thank you very much for this pleasing feedback! It means a lot to me!
Post a Comment
Please provide your valuable comment.