Wednesday, May 27, 2009

Membuat Aplikasi SystemTray dengan java


untuk membuat SystemTray seperti gambar diatas ternyata tidak sesulit yang saya kira.
berikut source code beserta penjelasanya.

Dowload project

/*
* Program ini dibuat oleh Rachmad Hadi Wijoyo
* anda boleh mengunakan dan mendistribusikan dengan bebas
* asalkan tidak mengubah license ini.
* copyright by Rachmad Hadi Wijoyo
*/

package cobacoba;

/**
*
* @author rahw
*/
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class RTray {
public static void main(String[] args) {
/* Set Look and Feel yang kita guanakan*/
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* mematikan evek metal look and feel */
UIManager.put("swing.boldMetal", Boolean.FALSE);

//Menjadwal event-dispatching thread:
//Memasang TrayIcon.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
new tesSystemTray().setVisible(true);
}
});
}

private static void createAndShowGUI() {
//Untuk memeriksa apakah sistem mendukung SystemTray
if (!SystemTray.isSupported()) {
System.out.println("Tidak mendukung SystemTray ");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon =
new TrayIcon(createImage("/cobacoba/administrator_128.png", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();

// membuat popup menu components
MenuItem aboutItem = new MenuItem("Tentang");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");


MenuItem exitItem = new MenuItem("Exit");

//Add components to popup menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb2);
popup.addSeparator();

popup.add(exitItem);

trayIcon.setPopupMenu(popup);

try {
tray.add(trayIcon);
trayIcon.setImageAutoSize(true);
} catch (AWTException e) {
System.out.println("TrayIcon tidak dapat dipasang.");
return;
}

trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new tesSystemTray().setVisible(true);
}
});

aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Pesan ini dijalankan dari System Tray\n"+
"Program ini dibuat oleh Rachmad Hadi Wijoyo\n"+
"anda boleh mengunakan dan mendistribusikan dengan bebas\n"+
"asalkan tidak mengubah license ini.\n"+
"copyright by Rachmad Hadi Wijoyo \n"
);
}
});


cb2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int cb2Id = e.getStateChange();
if (cb2Id == ItemEvent.SELECTED){
trayIcon.setToolTip("Sun TrayIcon");
} else {
trayIcon.setToolTip(null);
}
}
});


exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
System.exit(0);
}
});
}

//Obtain the image URL
protected static Image createImage(String path, String description) {
URL imageURL = RTray.class.getResource(path);

if (imageURL == null) {
System.err.println("Gambar tidak ditemukan di: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
}


___________

/*
* Program ini dibuat oleh Rachmad Hadi Wijoyo
* anda boleh mengunakan dan mendistribusikan dengan bebas
* asalkan tidak mengubah license ini.
* copyright by Rachmad Hadi Wijoyo
* anda boleh mengunakan dan mendistribusikan dengan bebas
*/

/*
* tesSystemTray.java
*
* Created on May 19, 2009, 11:05:11 AM
*/

package cobacoba;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
*
* @author rahw
*/
public class tesSystemTray extends javax.swing.JFrame {

/** Creates new form tesSystemTray */
public tesSystemTray() {
initComponents();
timerx.start();
}

/** 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")
// //GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(153, 153, 255));

jButton1.setText("Sembunyi");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Keluar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cobacoba/administrator_128.png"))); // NOI18N
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel1MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel1MouseExited(evt);
}
});

jLabel2.setText("copyright by Rachmad Hadi Wijoyo ");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(117, 117, 117)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(134, 134, 134)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(4, 4, 4))
);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-408)/2, (screenSize.height-327)/2, 408, 327);
}//
//GEN-END:initComponents

private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseEntered
// TODO add your handling code here:
jLabel1.setSize(jLabel1.getSize().width+20, jLabel1.getSize().height+20);
}//GEN-LAST:event_jLabel1MouseEntered

private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseExited
// TODO add your handling code here:
jLabel1.setSize(jLabel1.getSize().width-20, jLabel1.getSize().height-20);
}//GEN-LAST:event_jLabel1MouseExited

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
System.exit(0);
}//GEN-LAST:event_jButton2ActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tesSystemTray().setVisible(true);

}
});
}
Timer timerx = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent E) {
if (jLabel2.getLocation().x <= (-jPanel1.getWidth()-50)){ jLabel2.setLocation(jPanel1.getWidth(),jLabel2.getLocation().y ); jLabel2.setLocation(jLabel2.getLocation().x-10, jLabel2.getLocation().y); }else{ jLabel2.setLocation(jLabel2.getLocation().x-10, jLabel2.getLocation().y); } } }); // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; // End of variables declaration//GEN-END:variables } Download Project

No comments:

Post a Comment