Java TrayIcon message not showing
I am trying to get the main system tray message to show up in Windows 8.1 using TrayIcon. However, when the program starts, nothing appears. This is the code:
package alert1;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.imageio.*;
public class Main {
public static void main(String[] args) throws IOException {
URL gfl = new URL("http://gflclan.com/GFL/serverlist.php");
BufferedReader in = new BufferedReader(new InputStreamReader(gfl.openStream()));
Image img = ImageIO.read(new File("gflicon.jpg"));
TrayIcon tray = new TrayIcon(img);
System.out.println("Enter name of map: ");
Scanner scan = new Scanner(System.in); //retrieves name of map from IO
String str = scan.nextLine();
scan.close();
//pL = previousLine
String pL1 = null; //line which contains the server name
String pL2 = null;
String pL3 = null;
String pL4 = null; //line which contains the server IP
String pL5 = null;
String currentLine;
while ((currentLine = in.readLine()) != null)
if(currentLine.contains(str)){
String pL1fixed = pL1.replaceAll("\\<.*?\\> ?", "").trim(); //removes HTML/CSS formatting
String pL4fixed = pL4.replaceAll("\\<.*?\\> ?", "").trim();
System.out.println("Server Name: " + pL1fixed);
System.out.println("Server IP: " + pL4fixed);
tray.displayMessage("Server Found", "[Server Info Here]", TrayIcon.MessageType.WARNING);
} else {
pL1 = pL2; //updates stream line history
pL2 = pL3;
pL3 = pL4;
pL4 = pL5;
pL5 = currentLine;
}
in.close();
}
}
Is there something I am missing? As far as I can tell, I have a TrayIcon object and I called displayMessage, so I don't know why it doesn't appear. This is my first Java project and this is my first time working with images, so forgive me if this code is very amateurish.
source to share
First of all, have a look at How to use the system tray and JavaDocs forSystemTray
which have a few examples
Basically, you are not adding TrayIcon
to anything
SystemTray
Shortened example taken from JavaDocs
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = ...;
trayIcon = new TrayIcon(image, "Tray Demo");
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
}
Secondly, you really shouldn't mix console programs with a GUI, they have different ways of working that are usually incompatible with each other.
source to share