Implementing an image upload button using Jython & Swing - java

I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!

getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.

The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.

Related

How do I change image icon of a JFrame?

I looked at other answers and tried:
ImageIcon img = new ImageIcon(url);
setIconImage(img.getImage());
and:
URL imgURL = getClass().getClassLoader().getResource(url);
if (imgURL != null) {
System.out.println("Found icon image: "+imgURL);
Image image=Toolkit.getDefaultToolkit().getImage(imgURL);
setIconImage(image);
} else {
System.err.println("Could not find icon image");
}
Within the JFrame class and I put the image file in the resource folder, and also the same folder as my .java files and the root folder of my project and even included the "/" symbol at the beginning of the URL string but nothing is working. I was wondering if anyone tried it lately and got it working?
I don't know what's inside your url string but there is another way to set the icon to a JFrame:
frame.setIconImage((Image)ImageIO.read(getClass().getResourceAsStream("/imagename.png")));
The / symbol means that your path is relative to your project root.
EDIT:
JFrame.setIconImage(Image image) doesn't work on macOS.
Instead you can use Application.getApplication().setDockIconImage(Image image) of the com.apple.eawt.Application package to get it work. (as mentioned by #Valencia Starr)
I got it to work.
So apparently Macs come installed with a library and I also had to make sure I had Java 1.8 in my build path.
I imported the com.apple.eawt.Application package and used Application.getApplication().setDockIconImage(image) which works on macOS and not on Windows.

Custom ToolBar with Java Swing for Desktop

I have created a GUI with Java Swing and wanting to create a custom toolbar according to my modules. Below are the images am wanting to use:
These images are placed in the same level as the src folder within my application. I am aware that I can perhaps create a jar with these images so that I can easily access them from within my application but do not know how. I have spent hours trying to make this work.
Below is my GUI that I have created ad wanting to beautify with these images for the toolbar else create an array of labels that will act as a navigation but either approach I couldn't get it to work.
The code below was my last attempt on this:
JToolBar toolbar1 = new JToolBar();
ImageIcon client = new ImageIcon("clients.png");
ImageIcon timesheet = new ImageIcon("timesheets.png");
JButton clientTB = new JButton(client);
JButton timesheetTB = new JButton(timesheet);
toolbar1.add(clientTB );
toolbar1.add(timesheetTB);
add(toolbar1, BorderLayout.NORTH);
I even moved these images and placed them within the class that's calling them.
What could I be doing wrong, please help?
You have a look at the JavaDocs for ImageIcon(String), the String value is "a String specifying a filename or path"
This is a problem, because your images aren't actually files, any more, they have been embedded within your application (typically within the resulting jar file) and no longer be treated like "normal files".
Instead, you need to use Class#getResource which searches the application's classpath for the named resource, something like...
// This assumes that the images are in the default package
// (or the root of the src directory)
ImageIcon client = new ImageIcon(getClass().getResource("/clients.png"));
Now, I have a personal dislike for ImageIcon, because it won't tell you when the image is loaded for some reason, like it can't be found or it's the wrong format.
Instead, I'd use ImageIO to read the image
ImageIcon client = new ImageIcon(ImageIO.read(getClass().getResource("/clients.png")));
which will do two things, first, it will throw a IOException if the image can't be loaded for some reason and two, it won't return until the image is fully loaded, which is helpful.
See Reading/Loading an Image for more details

Error while getting image from resources folder

(First of all, I'm sorry If there is like a million questions like this. I've tried everything I can and this is driving me nuts!)
I'm trying to had an icon to a JButton but I keep getting an IllegalArgumentException caused by ImageIO.
Here's what I have:
//Other UI elements ^
JButton X = new JButton("Clear");
//com.oliveira.ux is the package name
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
Icon clear = new ImageIcon(img);
//More UI elements
The Icon is located under src/PACKAGE NAME/resource/. (I use eclipse).
I've tried to change the location on the code above (the one I wrote here was the last one I tried) but all I get is an IllegalArgumentException when I run de .jar. Any suggestions?
Many thanks in advance
Here's the full error message:
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.oliveira.ux.Main.<init>(Main.java:146)
at com.oliveira.ux.Main.main(Main.java:75)
... 5 more
This points to the ImageIO part in the code I wrote above.
The path appears to be wrong...
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
getResource isn't expecting the pack name, but the "path" to the resource from the context of the class path (so the path is appended to the class path elements)
Something like...
Image img = ImageIO.read(getClass().getResource("/com/oliveira/ux/resource/gtk-clear.png"));
Should give a better result

Adding images in java [duplicate]

I have 6 JButtons on my GUI all have images on it,
when I compile and run the code, all images on JButtons show up perfectly
but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?
I used this method in my code to show icons on JButtons
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false);
jb1.setContentAreaFilled(false);
This is how my GUI looks when I compile my code in Eclipse
This is how my GUI looks after executing Runnable JAR file
This (as pointed out by a number of people)
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.
ImageIcon won't complain if the file does not exist.
If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.
If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).
Just keep the jar and images in the same folder and
keep
ImageIcon icon = new ImageIcon("image.jpg");
in the code

Unable to get image icon in runnable jar

I wrote a method in order to get icon for my swing:
public Icon getIcon(String iconName) {
Icon icon = null;
if(iconName.equals("NEXT")){
icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );
}
return icon;
}
but
icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );
goes in null pointer
I created a source folder "resources" and a folder "img" inside it with "next.png" icon
Where's the problem?
Thanks
For this to work, the resources folder should be in the same folder as the folder corresponding to the package of this.getClass(). To start from the root of the classpath, use getClass().getResource("/resources/img/next.png"). (with a leading /)
so, I found the right method:
public static ImageIcon getImageIcon(String iconName) {
ImageIcon imageIcon = null;
if(iconName.equals("DOWNLOAD")){
imageIcon = new ImageIcon(ImagesLocation.class.getResource("/img/download.png"));
}
return imageIcon;
}
with a "resources" source folder at the same level of the project and with an img folder inside (package styled)
ImagesLocation is a generic class containing this method
For those in need of help that have come across this page in Google - I wrote an answer in another StackOverflow question giving the best way to handle images in JAVA apps so that you can easily access the images for all image method types in Java:
This IS the best way to handle all images and icons in a JAR App.
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
someimgicon = tk.getImage(iconUrl);
(someimgicon is just a constructor declared Image variable)
Now you can set a window icon as simply as:
setIconImage(someimgicon);
and at the same time use the same variable when setting the System TrayIcon by declaring:
trayIcon = new TrayIcon(someimgicon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
Much better in my view.

Categories