I have a program which when runs displays an icon in system tray. i am using the below code to display an icon in system tray area:
public static void showTrayIcon() {
if (java.awt.SystemTray.isSupported()) {
st = java.awt.SystemTray.getSystemTray();
image = Toolkit.getDefaultToolkit().getImage(PongeeUtil.class.getClass().getResource("export.png"));
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello");
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("sdf");
defaultItem.addActionListener(listener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
trayIcon.addActionListener(listener);
try {
st.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
}
}
When i call this method in my main() i got something in my system tray but icon is missing. i think image is not able to load. image is in the same package where my java files resides.
What am i doing wrong here ?
image is in the same package where my java files are
If you take a look at the JavaDocs for Toolkit#getImage you will find that it says...
Returns an image which gets pixel data from the specified file
This is important. You should also know that getImage loads the physical image in a background thread, which means if it fails to load the image, it will do so silently...
Okay. The core problem is, once the image is placed within the context of the application (with the class files), it becomes, whats commonly known as, an embedded resource.
These resources can not be loaded via any means that requires access to a file on the file system.
Instead, you need to use Class#getResource or Class#getResourceAsStream to load them, for example
image = Toolkit.getDefaultToolkit().getImage(YourClass.class.getResource("/package/path/to/classes/export.png"));
Or more preferabbly...
BufferedImage img = ImageIO.read(YourClass.class.getResource("/package/path/to/classes/export.png"));
image = new ImageIcon(img);
ImageIO will throw an IOException when it can't load the image for some reason, which gives you more diagnostic information to fix problems
nb:
YourClass is you class which contains the showTrayIcon method...
/package/path/to/classes is the package name under which the image is stored...
Related
I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.
Here's what I have at the moment (leaving out the try-catch block):
URL url = new URL("com/xyz/resources/camera.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);
The class that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.
Anyone have a solution that works?
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");
May or may not require a '/' at the front of the path.
You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:
Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))
Do not forget to import:
import java.awt.Toolkit;
in the source code!
Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.
com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );
That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.
Try This write after
initcomponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
/** Creates new form Java Program1*/
public Java Program1()
Image im = null;
try {
im = ImageIO.read(getClass().getResource("/image location"));
} catch (IOException ex) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
}
setIconImage(im);
This is what I used in the GUI in netbeans and it worked perfectly
In a class that extends a javax.swing.JFrame use method setIconImage.
this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());
You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.
public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");
List<Image> images = new ArrayList<>();
try {
images.add(ImageIO.read(HelperUi.ICON96));
images.add(ImageIO.read(HelperUi.ICON32));
images.add(ImageIO.read(HelperUi.ICON16));
} catch (IOException e) {
LOGGER.error(e, e);
}
// Define a small and large app icon
this.setIconImages(images);
You can try this one, it works just fine :
` ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
this.setIconImage(icon.getImage());`
inside frame constructor
try{
setIconImage(ImageIO.read(new File("./images/icon.png")));
}
catch (Exception ex){
//do something
}
Example:
URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png");
ImageIcon iChing = new ImageIcon("C:\\Users\\RrezartP\\Documents\\NetBeansProjects\\Inventari\\src\\Gui\\icon\\report-go-icon.png");
btnReport.setIcon(iChing);
System.out.println(imageURL);
The project runs fine when I click play in Eclipse but after I created the runnable JAR file, The ImageIcon for the button is gone.
The images are stored within the src folder, uder a subfolder images.
I created the image icon as
Icon playIcon = (Icon) new ImageIcon("src/images/play.png");
Although I am using a relative path, the button does not display images, How do I get the images even in the JAR file?
Update after Nikolay Kuznetsov's answer
I ended up creating a very unstructured monolithic code for screen recorder and now it is slightly difficult to implement what he said.
I was wondering if there is a way like creating a class or interface that will contain all these resources.
For example:
public class embeddedResources {
public static Icon blackCursor;
public static Icon whiteCursor;
...
...
}
Then all I have to do is import these statics and in my main ScreenRecorder class,
this.blackCursor = embeddedResources.blackCorsor;
I am using this method to read image into BufferedImage where IconManager is class where it is defined.
private static BufferedImage readBufferedImage (String imagePath) {
try {
InputStream is = IconManager.class.getClassLoader().getResourceAsStream(imagePath);
BufferedImage bimage = ImageIO.read(is);
is.close();
return bimage;
} catch (Exception e) {
return null;
}
}
I had the same problem.
Try to make you JAR file and add your images to an extern folder.
So you have a folder "Game" in this folder are the folder for your images called "images" or so and your jar file.
If you now edit you folder path to "../images/play.png" it should work.
Ok my problem is simple: i make Paint application, and i would like to provide some kind of "undo" functionality. I read about undo managers and so on, but this approach seems to be difficult to understand for me so i decided to design my own. My idea is simple: since i draw on JPanel i will just save its current content just before any drawing operation is made. And undo button will just restore previous one.
The trouble is: I am unable to "restore" the saved image and place it on the JPanel. I am buffled since i have already implemented "saving" the current image on harddrive and also "opening" arbitrary image from harddrive. Both of those work perfectly fine.
Unfortunately trying the exactly the same approach for my "undo" system does not work.
Look at the code: (to simplify, i manually 'save' the current picture -> for testing purposes)
public class PictureEdit { //class for saving the current JPanel drawing for undo
public BufferedImage saveBI;
public Graphics2D saveG2D;
}
public void actionPerformed(ActionEvent e) { //action for "UNDO" button, not working
//drawingField - instance of JPanel
//pe - instance of PictureEdit class
drawingField.setBufferedImg(drawingField.pe.saveBI);
drawingField.updateArea(drawingField.getBufferedImg());
}
public void actionPerformed(ActionEvent e) { //action for manual "save" (for undo)
drawingField.pe.saveBI=drawingField.getBufferedImg();
drawingField.pe.saveG2D=(Graphics2D)drawingField.getBufferedImg().getGraphics();
}
Now the example of my working solution but related to woking witk files on HDD:
public void actionPerformed(ActionEvent e){ //Opening file from harddrive - works fine
JFileChooser jfc = new JFileChooser();
int selection = jfc.showOpenDialog(PaintUndo.this);
if (selection == JFileChooser.APPROVE_OPTION){
try {
drawingField.setBufferedImg(ImageIO.read(jfc.getSelectedFile()));
drawingField.updateArea(drawingField.getBufferedImg());
} catch (IOException ex) {
Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(PaintUndo.this, "Could not open file");
}
}
}
public void actionPerformed(ActionEvent e) { //Saving to harddrive - works fine
int n = JOptionPane.showConfirmDialog(PaintUndo.this, "Are you sure you want to save?", "Saving image...", JOptionPane.OK_CANCEL_OPTION);
if (n == JOptionPane.YES_OPTION){
JFileChooser jfc = new JFileChooser();
int nn = jfc.showSaveDialog(PaintUndo.this);
if (nn == JFileChooser.APPROVE_OPTION){
File saveFile = new File(jfc.getSelectedFile()+".bmp");
try {
ImageIO.write(drawingField.getBufferedImg(), "bmp", saveFile);
} catch (IOException ex) {
Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(PaintUndo.this, "Error while saving file");
}
}
}
}
Just in case someone is curious: updateArea function (member of extended JPanel class that represents mentioned drawingField):
public void updateArea(BufferedImage img){ //it is just resizing the current picture (if necessary) and then assigns the new Graphics.
area.height=img.getHeight();
area.width=img.getWidth();
this.setPreferredSize(area);
g2d = (Graphics2D)this.getGraphics();
}
Basically the procedure is exactly the same, and working with files is ok while i cannot save and restore image when im operating on variables... What could be wrong? any ideas? When i use Undo/UndoSave buttons, absolutely nothing changes in my JPanel (drawingField)
Any help appreciated
In the code you've shown, you are copying your reference to the image, but both references still point at the same image object - if you perform any action which doesn't reassign the reference (=) it will be reflected in both references! In order to save an old version of the image, you need to copy the actual object, not just the reference.
I do recommend using Java's established undo management approach but if you want to continue with your own, these are methods you should look at:
//BufferedImage image;
Raster raster = image.getData(); // save
image.setData(raster); // restore
I have a JFileChooser that lets users choose an image for themselves. I want to limit the images they can choose to ones with square dimensions, for example -
width and height both 50
width and height both 75, etc...
So when they select an image with the JFileChooser and click 'Open' I need to validate the image size and if it doesn't have square dimensions I need to present the user with a dialog informing them "The image must have the same width and height".
I'm just learning swing so I don't know how to do this. Any ideas on how to do this? Is there a way of hooking the "Open" button's event handler?
You can hide all images that do not confirm to the rules with an implementation of a FileFilter:
JFileChooser fileChooser = new JFileChooser(new File(filename));
fileChooser.addChoosableFileFilter(new MyFilter());
// Open file dialog.
fileChooser.showOpenDialog(frame);
openFile(fileChooser.getSelectedFile());
class MyFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
// load the image
// check if it satisfies the criteria
// return boolean result
}
}
I tried overwriting
public void approveSelection ()
by deriving a own class from JFileChooser, and at first glance, it seemed to work.
The method is called, I can make a test on the selected file, and, if it fails, recall showOpenDialog (ref);.
But ...
It works fine, when I call a legitimate file, and it opens a new dialog, if not, but after that, the dialog won't close again normally, and if forced by the X of the window, I get a StackTrace printed. So I guess the state of the dialog is the critical thing here - it doesn't work if 'showOpenDialog' is called recursively.
Here is one of the variants I tested:
class ProportionalImageChooser extends JFileChooser
{
private Component ref;
public ProportionalImageChooser (File f)
{
super (f);
}
public int showOpenDialog (Component parent)
{
ref = parent;
return super.showOpenDialog (parent);
}
public void approveSelection () {
System.out.println ("approving selection!");
String fname = getSelectedFile ().getName ();
if (fname.matches (".*e.*")) {
cancelSelection ();
System.out.println ("Dialog: size doesn't match");
showOpenDialog (ref);
}
else super.approveSelection ();
}
}
To keep the test simple, I only tested the filename to include an 'e' or not.
So I suggest, use Boris' approach, and test your file after finishing the dialog. If it fails, immediately reopen a new one.
I need it to run without having the files exported to the computer.
At the moment, my code for storing the images is:
ImageIcon icon = new ImageIcon("images\\images2.gif");
It can't just be an image since I'm adding it to a JLabel.
When I jar the entire program, it stores the image files in the jar.
When I go to run the actual problem, there are no images.
Again, I can't just leave the .jar in a folder with the images already. It has to work on a separate computer, by itself.
You'll want to get the image via the system class loader:
URL url = ClassLoader.getSystemClassLoader().getResource("images/images2.gif");
Icon icon = new ImageIcon(url)
images is at the root of the classpath.
Note that the Java runtime will translate the separator (/) to the OS specific separator (\ for Windows).
You need to access those files through class-loader... Something like this:
InputStream is = this.getClass().getClassloader().getResourceAsStream("images/image.ico");
HTH
UPD: note, that this will work both with JARed package and with plain directory structure.
The basic issue is that the File class only knows how to work with what the underlying operating system consider a file, and a whole one.
A jar file is essentially a zip file with some extra information so you cannot use File's with that. Instead Java provides the "resource" concept which roughly translates to "a chunk of bytes or characters which we don't care where is, as long as we have them when we need them". You can ask the class loader for any resource in the class path - which is what you want here - or access it through an URL.
Try this:
ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("images/images2.gif"));
if that doesn't work, replace this.getClass().getClassloader() with MyClass.class where MyClass is the name of your class.
I remember having to edit this slightly to make it work in Eclipse, but when you deploy it, it works like a charm.
Edit: To make it work in Eclipse, you may need to change it to:
ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("bin/images/images2.gif"));
If that doesn't work, do the standard, replace this.getClass().getClassloader() with MyClass.class. If it still doesn't work, try replacing "bin" with "src". Try jar'ing it with the first way and see what happens.
Here is a convenient utility class that can be used for loading image resources. The log4j logger can be removed of changed to whatever is more appropriate.
public class ResourceLoader {
private static final Logger logger = Logger.getLogger(ResourceLoader.class);
public static Image getImage(final String pathAndFileName) {
try {
return Toolkit.getDefaultToolkit().getImage(getURL(pathAndFileName));
} catch (final Exception e) {
logger.error(e.getMessage());
return null;
}
}
public static ImageIcon getIcon(final String pathAndFileName) {
try {
return new ImageIcon(getImage(pathAndFileName));
} catch (final Exception e) {
logger.error(e.getMessage());
return null;
}
}
public static URL getURL(final String pathAndFileName) {
return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
}
}
I suppose images2.gif is inside the package images
URL imageurl = getClass().getResource("/images/images2.gif");
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
If your images are located this way.
Then this should do the job.
private String imageLocation "/images/images2.png";
private ImageIcon getImageIconFromJar(String imageLocation)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass().getResource(imageLocation));
ImageIcon ic = new ImageIcon(bi);
return ic;
} catch (IOException e)
{
System.out.println("Error: "+e);
}
return null;
}