Last active
August 29, 2015 14:03
-
-
Save ochinchina/573816ef98d70f43e866 to your computer and use it in GitHub Desktop.
Demo the System Tray in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get these files and copy it to a directory | |
# Get a .png file and rename it to coffee.png and also put the the same directory as the source file | |
# compile and run it with command: | |
$ java -cp . WindowNotifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author Steven Ou | |
*/ | |
public class TestFrame extends javax.swing.JFrame { | |
/** | |
* Creates new form TestFrame | |
*/ | |
public TestFrame() { | |
initComponents(); | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
jLabel1 = new javax.swing.JLabel(); | |
jScrollPane1 = new javax.swing.JScrollPane(); | |
jTextArea1 = new javax.swing.JTextArea(); | |
jButton1 = new javax.swing.JButton(); | |
jButton2 = new javax.swing.JButton(); | |
jLabel1.setText("Please enter your text"); | |
jTextArea1.setColumns(20); | |
jTextArea1.setRows(5); | |
jScrollPane1.setViewportView(jTextArea1); | |
jButton1.setText("Save"); | |
jButton1.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton1ActionPerformed(evt); | |
} | |
}); | |
jButton2.setText("Exit"); | |
jButton2.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton2ActionPerformed(evt); | |
} | |
}); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(28, 28, 28) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel1) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addContainerGap(21, Short.MAX_VALUE)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButton1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButton2) | |
.addGap(24, 24, 24)) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(27, 27, 27) | |
.addComponent(jLabel1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jButton1) | |
.addComponent(jButton2)) | |
.addContainerGap(17, Short.MAX_VALUE)) | |
); | |
pack(); | |
}// </editor-fold> | |
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
} | |
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { | |
this.setVisible( false ); | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new TestFrame().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton jButton1; | |
private javax.swing.JButton jButton2; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JScrollPane jScrollPane1; | |
private javax.swing.JTextArea jTextArea1; | |
// End of variables declaration | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.AWTException; | |
import java.awt.Dimension; | |
import java.awt.Image; | |
import java.awt.MenuItem; | |
import java.awt.PopupMenu; | |
import java.awt.SystemTray; | |
import java.awt.Toolkit; | |
import java.awt.TrayIcon; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.net.URL; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import javax.swing.JFrame; | |
/** | |
* | |
* @author Steven Ou | |
*/ | |
public class WindowNotifier { | |
private static final String IMAGE_NAME = "coffee.png"; | |
private final SystemTray systemTray = SystemTray.getSystemTray(); | |
private final TrayIcon icon; | |
private final Timer timer = new Timer(); | |
private final JFrame frame = new TestFrame(); | |
public WindowNotifier() throws AWTException { | |
Toolkit tk = Toolkit.getDefaultToolkit(); | |
PopupMenu popMenu = new PopupMenu(); | |
MenuItem item = new MenuItem("Exit"); | |
item.addActionListener( new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
System.exit( 0 ); | |
} | |
}); | |
popMenu.add( item ); | |
item = new MenuItem("Show Context"); | |
item.addActionListener( new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
frame.setVisible( true ); | |
} | |
}); | |
popMenu.add( item ); | |
this.icon = new TrayIcon( getImage( IMAGE_NAME ), "test", popMenu ); | |
this.icon.setImageAutoSize(true); | |
this.systemTray.add( this.icon ); | |
this.timer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
WindowNotifier.this.icon.displayMessage( "New Message", "Current time is:" + new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS").format( Calendar.getInstance().getTime() ), TrayIcon.MessageType.INFO ); | |
} | |
}, 5*1000, 10*1000 ); | |
centerFrame(); | |
} | |
private Image getImage( String name ) { | |
URL url = getClass().getResource(name); | |
return Toolkit.getDefaultToolkit().getImage(url); | |
} | |
private void centerFrame() { | |
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); | |
int x = (int)( ( d.getWidth() - frame.getWidth() ) / 2); | |
int y = (int)( ( d.getHeight() - frame.getHeight() ) / 2); | |
frame.setLocation( x, y ); | |
} | |
public static void main( String...args ) throws AWTException { | |
new WindowNotifier(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment