import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
public class trayform extends javax.swing.JFrame {
private static trayform obj = null;
private trayform() {
initComponents();
JPopupMenu popup = new JPopupMenu();
loadPopup(popup);
setTI();
}
public static trayform getobj() {
if (obj == null) {
obj = new trayform();
}
return obj;
}
public trayform(JPopupMenu popup, int x, int y) {
// Window win = new Window(new Frame()); //null);
JDialog win = new JDialog(new Frame()); //null);
win.setUndecorated(true);
JMenuBar mnBar = new JMenuBar(); //Works
final JMenu men = new JMenu("Test"); //<< Shows Test and a Right caret
JMenuItem mi1 = new JMenuItem("one"); //
men.add(mi1);
mnBar.add(men);
win.setLayout(new BorderLayout());
// win.add(mnBar, BorderLayout.NORTH);
win.setBounds(x, y, 100, 100);
win.setOpacity(0.05f);
// win.setBackground(
win.setVisible(true);
win.setFocusable(true);
// loadPopup(popup);
win.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("mC at " + e.getPoint()); // WORKS
// Place and show popup
popup.setLocation(e.getPoint());
popup.setVisible(true);
if (e.getPoint().y > 90) {
System.exit(0);
}
}
});
mnBar.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent evt) {
System.out.println("focusLost"); //<<<< never
popup.setVisible(false);
}
@Override
public void focusGained(FocusEvent evt) {
System.out.println("focusGained"); // ditto
}
});
win.addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowLostFocus(WindowEvent evt) {
System.out.println("WfocusLost"); //<<<< THIS ONE
popup.setVisible(false);
}
@Override
public void windowGainedFocus(WindowEvent evt) {
System.out.println("WfocusGained"); // ditto
}
});
win.requestFocus(); // nothing
men.requestFocusInWindow(); // nothing
System.out.println("isFocusable()=" + win.isFocusable()); // isFocusable()=true
}
//----------------------------------------------
TrayIcon ti;
public void setTI() {
try {
ti = new TrayIcon(ImageIO.read(getClass().getResource("/images/kabe.jpg")), "Have a nice day");
ti.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("mC for ti at " + e.getPoint());
// What x,y for the popup????
int x = e.getPoint().x;
int y = e.getPoint().y;
// Create our own local popup to respond to TrayIcon click
JPopupMenu popup = new JPopupMenu();
new trayform(popup, x - 20, y - 20);
loadPopup(popup);
popup.setLocation(x - 12, y - 95);
popup.setVisible(true);
}
});
SystemTray.getSystemTray().add(ti); //<<<<<<<
} catch (Exception ex) {
ex.printStackTrace();
}
}
//--------------------------------------------------------------------------
public void loadPopup(JPopupMenu popup) {
JMenuItem Exit = new JMenuItem("خروج");
Exit.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); //exit.png"))); // NOI18N
JMenuItem Open = new JMenuItem("نمایش");
Open.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/kabe.jpg"))); //kabe.jpg"))); // NOI18N
JMenuItem Info = new JMenuItem("درباره ما");
Info.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
Info.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/info.png"))); //info.png"))); // NOI18N
Info.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
Info.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
Info.setBackground(UIManager.getColor("control"));
}
});
Exit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
Exit.setBackground(Color.RED);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
Exit.setBackground(UIManager.getColor("control"));
}
});
Open.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
Open.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
Open.setBackground(UIManager.getColor("control"));
}
});
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JFrame frame = new JFrame();
// frame.
setVisible(true);
popup.setVisible(false);
// frame.
setExtendedState(JFrame.NORMAL);
}
});
Info.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ti.displayMessage("Open form", "Frame is opened", TrayIcon.MessageType.NONE);
}
});
popup.add(Open);
popup.addSeparator();
popup.add(Info);
popup.addSeparator();
popup.add(Exit);
// popup.setComponentOrientation(ComponentOrientation .RIGHT_TO_LEFT);
popup.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
} // end loadPopup
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowStateListener(new java.awt.event.WindowStateListener() {
public void windowStateChanged(java.awt.event.WindowEvent evt) {
formWindowStateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setSize(new java.awt.Dimension(416, 339));
setLocationRelativeTo(null);
}// </editor-fold>
private void formWindowStateChanged(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
if (evt.getNewState() == ICONIFIED) {
//System.out.println("unable to add to tray");
setVisible(false);
}
if (evt.getNewState() == 7) {
//System.out.println("unable to add to system tray");
setVisible(false);
}
if (evt.getNewState() == MAXIMIZED_BOTH) {
setVisible(true);
//System.out.println("Tray icon removed");
}
if (evt.getNewState() == NORMAL) {
setVisible(true);
//System.out.println("Tray icon removed");
}
}
public static void main(String args[]) {
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(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new trayform().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}