Add some methods ...
private static boolean isJpeg(String ext) { return java.util.Arrays.asList("jpg", "jpeg").contains(ext.toLowerCase()); } private static boolean isPng(String ext) { return "png".equalsIgnoreCase(ext); } private static boolean isBmp(String ext) { return "bmp".equalsIgnoreCase(ext); }
And change it to ...
else if (isJpeg(extension) || isPng(extension) || isBmp(extension)) { tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png"))); }
isJpeg will isJpeg NullPointerException if the extension is null, so make sure it is not null by adding extension != null || ... extension != null || ... or something like that.
The above is slightly different for your specific case, as it allows JpEg and all other mixed capitalizations to slip. If you do not want this, use them. In addition, the following has the added benefit of never throwing a NullPointerException if the extension is null .
private static boolean isJpeg(String ext) { return java.util.Arrays.asList("jpg", "JPG", "jpeg", "JPEG").contains(ext); } private static boolean isPng(String ext) { return java.util.Arrays.asList("png", "PNG").contains(ext); } private static boolean isBmp(String ext) { return java.util.Arrays.asList("bmp", "BMP").contains(ext); }
source share