how do you simply put an image(gif format) to an Image datatype?
example:
import java.awt.image.*;
Image i = ?????
how do you do that?
Printable View
how do you simply put an image(gif format) to an Image datatype?
example:
import java.awt.image.*;
Image i = ?????
how do you do that?
Something like this, add an image to GUI or something...
Code:Image image = Toolkit.getDefaultToolkit().getImage(your_image_path);
wow, thanks a lot. by the way, is there a simpler way to make an item in the system tray? by the way, i was the one who asked you last week regarding threads that run in an infinite loop. setting the timeout really does well. from 100% CPU it comes down to only 12-50%.
we are making an IM and maybe you have an idea how to make a system tray for that? that is why i asked how to put an image to an Image datatype cause it is a parameter for a certain method creating a system tray. is there a simpler way on doing it? i find it quite difficult to understand all on the net. thanks for the reply anyways. :)
make an item, or make an image in the system tray? Normally an item mean a system tray pop-up menu item.
How did you do it, because I don't know that ;).
there is this class SystemTray i think in JAVA SE 6. located at java.awt. not quite sure though. here is a link to it on java forums:
New System Tray Functionality in Java SE 6
i dont know if its illegal here posting external links but if its illegal ill just edit it
i'm not that smart to understand it, hehe
First thing is, yes you can post links here in our community. But in good way, don't directed people in wrong way. ;)
Ok, you found a example here. This is a new feature of Java SE6. Before this it's not much easy. :) So now you can go ahead with that example. Where you stuck with.
can you tell whats the problem with this code?
Quote:
import java.awt.event.*;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.TrayIcon.*;
public class TrayIconDIMS
{
SystemTray systemtray;
java.awt.Image image;
TrayIcon trayIcon;
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
public void TrayIconDIMS()
{
image = Toolkit.getDefaultToolkit().getImage("724793.gif") ;
}
public void showTray()
{
try
{
systemtray = SystemTray.getSystemTray();
System.out.println("System Tray Initialized");
}
catch(java.lang.UnsupportedOperationException e)
{
e.printStackTrace();
System.out.println("Unable to initialize System tray");
}
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Performed!",
TrayIcon.MessageType.INFO);
}
};
}
public static void main(String args[])
{
TrayIconDIMS A = new TrayIconDIMS();
A.showTray();
}
}
the error:
Quote:
init:
deps-jar:
Compiling 1 source file to /home/byuu/NetBeansProjects/testest/build/classes
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:78: cannot find symbol
symbol : constructor TrayIcon(java.awt.Image,java.lang.String,java.awt. PopupMenu)
location: class TrayIcon
trayIcon = new TrayIcon(image, "Tray Demo", popup);
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:84: cannot find symbol
symbol : variable MessageType
location: class TrayIcon
TrayIcon.MessageType.INFO);
2 errors
BUILD FAILED (total time: 0 seconds)
Could you try this to your code. =) I think its the same error in handling arrays. In my experience with that, its the declaration. I hope this helps
Code:SystemTray systemtray;
java.awt.Image image = Toolkit.getDefaultToolkit().getImage("724793.gif") ;
TrayIcon trayIcon = new TrayIcon(image);
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
it still get the same error.
and get another error at your code:
i think the problem is in the TrayIcon declaration or somethin'. thanks for the quick reply.. hope we could sort this one out hehe :)Quote:
TrayIcon trayIcon = new TrayIcon(image);
im gonna try to compile it on windows. im running on linux,
ohhh. . . Im sorry, Maybe the senior members can help us. =P
You have done it in wrong way, I mean the order you have to take is incorrect. Here is the modified code. Try to get the difference between this and your code.
Code:import java.awt.event.*;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.TrayIcon.*;
public class TrayIconDIMS
{
SystemTray systemtray;
Image image; TrayIcon trayIcon;
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
public void TrayIconDIMS() {
}
public void showTray() {
if (SystemTray.isSupported()) {
try {
systemtray = SystemTray.getSystemTray();
System.out.println("System Tray Initialized");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
//defaultItem.addActionListener(exitListener);
//popup.add(defaultItem);
image = Toolkit.getDefaultToolkit().getImage("RADAR.png");
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(exitListener);
systemtray.add(trayIcon);
}
catch (AWTException ex) {
System.out.println("Error in icon loading.");
}
}
else {
System.out.println("System tray not supported.");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TrayIconDIMS().showTray();
}
});
}
}
sorry for the late reply. i havent have quite time programming since there was a transport strike here and its hard riding a public transportation. hm, i tried the code, compiled it and says:
the code seems no sign of error in syntax or usage but i dont know if we are missing anything..Quote:
init:
deps-jar:
Compiling 1 source file to /home/byuu/NetBeansProjects/testest/build/classes
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:56: cannot find symbol
symbol : constructor TrayIcon(java.awt.Image,java.lang.String,java.awt. PopupMenu)
location: class TrayIcon
trayIcon = new TrayIcon(image, "Tray Demo", popup);
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:60: cannot find symbol
symbol : variable MessageType
location: class TrayIcon
trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", TrayIcon.MessageType.INFO);
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:64: cannot find symbol
symbol : method setImageAutoSize(boolean)
location: class TrayIcon
trayIcon.setImageAutoSize(true);
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:65: cannot find symbol
symbol : method addActionListener(java.awt.event.ActionListener)
location: class TrayIcon
trayIcon.addActionListener(exitListener);
/home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:66: add(java.awt.TrayIcon) in java.awt.SystemTray cannot be applied to (TrayIcon)
systemtray.add(trayIcon);
5 errors
BUILD FAILED (total time: 0 seconds)
ney, i kinda fixed it. it displays a trayicon on the system tray although empty. the problem i son the imports. i took away the
import java.awt.TrayIcon
and just declared the whole thing on instances, method calls of class TrayIcon here is the code, just thinking i could share it here, i don't know what's the difference but it worked anyway, :)
Quote:
import java.awt.event.*;
import java.awt.*;
import java.awt.Toolkit;
//import java.awt.TrayIcon.*;
public class TrayIconDIMS
{
SystemTray systemtray;
Image image;
java.awt.TrayIcon trayIcon;
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
public void TrayIconDIMS() {
}
public void showTray() {
if (SystemTray.isSupported()) {
try {
systemtray = SystemTray.getSystemTray();
System.out.println("System Tray Initialized");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
//defaultItem.addActionListener(exitListener);
//popup.add(defaultItem);
image = Toolkit.getDefaultToolkit().getImage("RADAR.png");
trayIcon = new java.awt.TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", java.awt.TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(exitListener);
systemtray.add(trayIcon);
}
catch (AWTException ex) {
ex.printStackTrace();
}
}
else {
System.out.println("System tray not supported.");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TrayIconDIMS().showTray();
}
});
}
}
byuu, Are from the Phillipines? I didnt feel the Transport Strike Yesterday Morning. But later in the afternoon, there are lots of people in the streets.
yes, i'm from davao city, people throw spikes on the roads here, :))
There is no difference between following two lines.
I think you get the empty try icon, because you don't change the image name. Is it?Code:import java.awt.TrayIcon.*;
java.awt.TrayIcon trayIcon;
My code is working 100% perfectly. Which JDk version you used. This is support from 1.5 and seems to me you have older version than this.
If you want try this, you can see four import statements in my original code. Remove all of them and add only these two.
Still my code working fine. Still you get the error, you used older version of JDK.Code:import java.awt.event.*;
import java.awt.*;
The Strike wasnt succesful like before. Luckily i used the Train.
Im from Makati. Nice to meet you sir byuu.
yes mine is working on now. by the way i am using jdk 1.6 maybe java versions matter.. im not sure though
....
yes, nice to meet you too. :)Quote:
The Strike wasnt succesful like before. Luckily i used the Train.
Im from Makati. Nice to meet you sir byuu.
Ok, if you solved the question please mark thread solved.
And also don't make chit-chats on a thread. You have separate forum for that, just do to the forum lobby. :)