Question

I have the following code to open a window notification but I want the part that says "click here" to link to a text file. How do I add this function into the trayicon?

public class foundDocs implements ActionListener {


public static void main(String[]args) throws AWTException
{
    new foundDocs();
}
foundDocs() throws AWTException 
   { 
       SystemTray tray = SystemTray.getSystemTray(); 
        java.awt.Image image = Toolkit.getDefaultToolkit().getImage("tray.gif"); 
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo"); 
        tray.add(trayIcon); 
        trayIcon.displayMessage("Found new document associations:", "Click here to view", MessageType.INFO); 
        trayIcon.addActionListener(this);
    }

@Override
public void actionPerformed(ActionEvent arg0) 
{
    // display the text file in the default app. 
    try {
        Desktop.getDesktop().open(new File("Users.txt"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

}

}

Was it helpful?

Solution

Add an ActionListener to the TrayIcon. On event, use something like:

// display the text file in the default app.
Desktop.getDesktop().open(new File("the.txt"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top