Reputation: 27
I found this code and I'm trying to adapt it to my project.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
public class Album {
static boolean contr = false;
static String path = "photos/";
static int MAX;
static String []imgName;
static JLabel label = new JLabel();
static JMenuBar mBar = new JMenuBar();
static JMenu mn = new JMenu("open here ");
static JMenuItem []menuItem;
static JFrame fot = new JFrame();
static JButton back = new JButton(" back ");
public static void Album() {
if (contr==true){
fot.setVisible(true);
}
else{
JFrame.setDefaultLookAndFeelDecorated(true);
File directory = new File(path);
if (!directory.exists()) {
System.err.println("The specified directory doesn't exist!!");
err.err("fail.jpg");
}
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Do nothing
}
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
mBar.updateUI();
mn.updateUI();
// Filter out the Images
imgName = directory.list(new FilenameFilter() {
String[] readFormat = ImageIO.getReaderFormatNames();
@Override
public boolean accept(File dir, String name) {
for (int i = 0; i < readFormat.length; i++) {
if (name.endsWith(readFormat[i])) {
return true;
}
}
return false;
}
});
MAX = imgName.length;
if (MAX == 0) {
System.err.println("OOps , no images");
System.exit(-1);
}
//Limit the maximunm entries to 10
if (MAX > 19) {
MAX = 19;
}
menuItem = new JMenuItem[MAX];
for (int i = 0; i < menuItem.length; i++) {
menuItem[i] = new JMenuItem(imgName[i].substring(0, imgName[i].lastIndexOf(".")), new ImageIcon(getImage(path + imgName[i]).getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
menuItem[i].updateUI();
mn.add(menuItem[i]);
menuItem[i].setActionCommand(imgName[i]);
menuItem[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
label.setIcon(new ImageIcon(getImage(path + ae.getActionCommand())));
}
});
}
mBar.add(mn);
fot.setJMenuBar(mBar);
fot.add(new JScrollPane(label));
fot.add(back, BorderLayout.AFTER_LAST_LINE);
Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
fot.setSize(scrDim.width-250 , scrDim.height-50);
fot.setVisible(true);
fot.setTitle("photo album");
fot.setResizable(false);
fot.setLocationRelativeTo(null);
fot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
back.addActionListener(new ActionListener() { //back
@Override
public void actionPerformed(ActionEvent e) {
fot.setVisible(false);
contr=true;
menu.menu();
}
});
}
}
// Get the Image from the disk
public static Image getImage(String fileName) {
try {
return ImageIO.read(new File(fileName));
} catch (IOException ioe) {
System.err.println("Error loading Image : " + fileName);
}
return null;
}
}
I have to export all in a .jar file, running it from eclipse work properly, while once exported .jar file is not working. The problem is that I can not find the photos/ folder , it is a second folder of resources, but also trying to the root is not working. Instead, it can read both of eclipse that in .jar file, each image passed as a parameter, as for calling the next class that displays an error message in a image:
err.err("fail.jpg");
the pattern of project folders is as follows:
project:
All pathname are correct, als "fail.jpg". Are weeks that I'm looking for a solution, I need to know how to upload images directly from a folder of resources to make them work directly in the .jar file. thanks a lot!
Upvotes: 0
Views: 96
Reputation: 481
You should consider using getResourceAsStream("/fail.jpg")
method to get your file. An example would look like
InputStream inputStream = Main.class.getResourceAsStream("/fail.jpg");
Your jpg file should be in your source folder for this example.
Instead of Main you can use any class in your jar-file.
Upvotes: 1