Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created September 20, 2013 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resarahadian/6638424 to your computer and use it in GitHub Desktop.
Save resarahadian/6638424 to your computer and use it in GitHub Desktop.
System Tray App
import java.awt.EventQueue;
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 javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class frameTrayIcon extends JFrame
{
private JPanel contentPane;
private SystemTray tray;
private MenuItem mi;
private PopupMenu popUp;
private Image img;
private TrayIcon trayIcon;
private JTextField txtNim;
private JTextField txtNama;
@SuppressWarnings("rawtypes")
private JComboBox comboBox;
private JLabel lblWall;
private JLabel lblIcon;
/**
* Create the frame.
*/
@SuppressWarnings({ "static-access", "rawtypes", "unchecked" })
public frameTrayIcon()
{
super("System Tray App");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 499, 306);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNim = new JLabel("NIM : ");
lblNim.setBounds(12, 12, 70, 15);
contentPane.add(lblNim);
txtNim = new JTextField();
txtNim.setBounds(125, 10, 165, 28);
contentPane.add(txtNim);
txtNim.setColumns(10);
JLabel lblNama = new JLabel("Nama : ");
lblNama.setBounds(12, 52, 70, 15);
contentPane.add(lblNama);
txtNama = new JTextField();
txtNama.setBounds(125, 50, 271, 27);
contentPane.add(txtNama);
txtNama.setColumns(10);
JLabel lblJurusan = new JLabel("Jurusan : ");
lblJurusan.setBounds(12, 94, 70, 15);
contentPane.add(lblJurusan);
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"TI", "SI"}));
comboBox.setBounds(125, 89, 175, 33);
contentPane.add(comboBox);
lblIcon = new JLabel("");
lblIcon.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/TrayIcon Img/Comp.png"));
lblIcon.setBounds(146, 145, 190, 104);
contentPane.add(lblIcon);
lblWall = new JLabel("");
lblWall.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/TrayIcon Img/blurLight.jpg"));
lblWall.setBounds(0, -13, 497, 288);
contentPane.add(lblWall);
setLocationRelativeTo(null);
if(tray.isSupported())
{
tray = SystemTray.getSystemTray();
img = Toolkit.getDefaultToolkit().getImage("src/Gambar/TrayIcon Img/home.png");
popUp = new PopupMenu();
mi = new MenuItem("Close");
popUp.add(mi);
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
System.exit(0);
}
});
trayIcon = new TrayIcon(img,"Close,",popUp);
try
{
tray.add(trayIcon);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
frameTrayIcon frame = new frameTrayIcon();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment