Change System tray Icon in java - java

I just want to change the system tray Icon image for my application. I did 2 things -
Just changed the URL in the default program -
final TrayIcon trayIcon = new TrayIcon(createImage("images/Graph.png", "tray icon"));
Second try -
Image img = Toolkit.getDefaultToolkit().getImage("images/Graph.png");
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
The application launches in both cases but no image is shown. Its a blank placeholder. What am i doing wrong ?

images/Graph.png is not a valid URL for an image located in your jar. Hence, I guess that img is null on your second try.
I suggest you this way :
//Get the URL with method class.getResource("/path/to/image.png")
URL url = System.class.getResource("/images/Graph.png");
//Use it to get the image
Image img = Toolkit.getDefaultToolkit().getImage(url);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
You shall also ensure that images/ is in your classpath.

The problem is the way you include the image file as the image is inside your . jar, use getResource() or getResourceAsStream, try this:
try {
InputStream inputStream= ClassLoader.getSystemClassLoader().getResourceAsStream("/images/Graph.png");
//or getResourceAsStream("/images/Graph.png"); also returns inputstream
BufferedImage img = ImageIO.read(inputStream);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
}
catch (IOException e) {}

For me works in the next way:
// URL: MyProject/src/MyGUI/check.png
// Small icon on secondary taskbar
Image image= ImageIO.read(getClass().getResource("/MyGUI/check.png"));
trayIcon = new TrayIcon(image, "App name", popup);
trayIcon.setImageAutoSize(true);
Results

Related

codename one signatueComponent image upload

i have be trying to upload the signature captured from the codename one signature to my php server.the problem is that the image uploaded is a black image.Below is my code.how can i fix this
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
and the php code
[![image uploaded][1]][1]
<?php
#SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
the black img is the file which is uploaded to the server.the other shows the screenshot of the running app.i would like to upload the signature as shown in the screenshot
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage will be opaque with the white color background.

system tray icon not displaying on startup of program

I used following code for display system tray icon and message. Startup message and tooltip message is displayed well. But tray icon is not displaying. The icon is in images folder. How can i solve this problem ?
public void systemTray() {
try {
SystemTray tray = SystemTray.getSystemTray();
ImageIcon icon=new ImageIcon(getClass().getResource("/images/Reg Member MO.png"));
Image image = icon.getImage();
TrayIcon icn = new TrayIcon(image, "This is demonstration system tray");
icn.setToolTip("Now you can see system tray\ntooltip here\nThis is demonstration system tray ToolTip");
tray.add(icn);
icn.displayMessage("This is demonstration System Tray message", "You can add some text to\ndisplay here as System Tray Message", TrayIcon.MessageType.INFO);
} catch (AWTException ex) {
Logger.getLogger(StartupSystemTray.class.getName()).log(Level.SEVERE, null, ex);
}
}
May be your icon image is too large than allocated space. Try using public void setImageAutoSize(boolean autosize) method as icn.setImageAutoSize(true). This will re-size the image.

Error showing images in applet in browser

I'm new to Java Applets and I have a little problem.
I need to load and display images in applet loaded in browser.This is part of the code i'm using to display image:
Panel first=new JPanel();
URL url = null;
try {
url = new URL(new URL(path),"Dell_laptop.jpg");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Image img = getImage(url);
ImageIcon fimg = new ImageIcon(img);
JLabel jLabel1=new JLabel(fimg);
first.add(jLabel1);
So when it's working in eclipse, it's working flawless.But when i'm trying to load this same applet in HTML page, it's not working.Applet is loaded,but images aren't shown.Path is correct, checked it.

How to get resources working in runnable jar?

I have a Image Icon in my Jframe.I am using a Image in it.When i export into a Runnable Jar.The Image is Not displayed.
String iPath = "res/images/Mobile.png";
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, 315, 610);
InputStream stream = (InputStream) getClass().getResourceAsStream(iPath);
JLabel mobileImageLabel;
mobileImageLabel = new JLabel(new ImageIcon(iPath));
//mobileImageLabel = new JLabel(new ImageIcon(ImageIO.read(stream)));
// mobileImageLabel = new JLabel(new ImageIcon(AppFrame.class.getResource(iPath)));
mobileImageLabel.setBounds(0, 0, 315, 610);
mobileImageLabel.setVisible(true);
layeredPane.add(mobileImageLabel, Integer.valueOf(0));
I googled & found the getResourceAsStream method.But it seems to throw NullPointerException.
So Help me in the Right Direction :)
Thanks for your Help ...
Note
The Method i Tried has been Commented
To set a image in a button I did this:
JButton yourButton = new JButton("text button");
try {
yourButton.setIcon(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png"))));
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
You must have the images inside your project like this:
In your case, the answer I think is to do this:
new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png")))
Try to replace
String iPath = "res/images/Mobile.png";
By
String iPath = "C:/..../res/images/Mobile.png";
Whenever you are working with images try to put the complete path for the image. So when you will generate your .jar file and try to run it from different place(may be possible that you want the jar should be run from desktop) you will get all the images.

How to show icon file in system tray?

I am adding my application in system tray when I close it. But it is not displaying the icon. When I try to show picture file then it works fine but when I try with an icon file it does not work. How can I display an icon instead picture?
Image image = new ImageIcon("src/resources/busylogo.jpg").getImage();
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
Better use Toolkit to load an icon.
That's a low size file, and an asynchronous load will give you less problems.
Try this code, which is recommanded by Sun.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
tray.add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
More informations here : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

Categories

Resources