I'm trying to get the image file path or file name of an ImageIcon. I've created a screen in my Java gui app, which contains properties for a custom JButton (extends JButton). From that screen I'm setting some of the main button properties, as if it is enabled, focusable and its ImageIcon. The problem is that as if now, whenever I use this button class, which is in every screen btw, I'm loading all possible images for ImageIcons when the exdened JButton class is used. That causes the screen to freeze before any of the components are shown, while the images are loaded from classpath. In order to change that, in the settings screen, where I hava a JComboBox, containing all images for icons, there, at least that's what I can think of, should be a way to get only the name of the chosen ImageIcon- image path.
The buttons properties are stored in a properties file and that's where I intend to store the .png images names if I can get to them. The idea is to set the image name and when the button is loaded to the screen to look for and load only the image it's supposed to.
Here is a snipet of the button class now; The images are way more, but for demo purposes, I think those are enough. I'd be very grateful if anyone can help with this matter.
public class CustomButton extends JButton {
static Properties FieldProp;
public CustomButton (String text, String name) {
FieldProp = new LoadProperties().loadMainProp();
this.setText(text);
this.setName(name);
Image imgdel=null;
Image imgsmbl=null;
Image imgsmrd=null;
Image imgsmgr=null;
Image imgadd=null;
Image imgauto=null;
Image imgauto1=null;
Image imgavail=null;
Image imgbarc=null;
Image imgdb=null;
Image imgdoc=null;
Image imgexc=null;
Image imgexc1=null;
try {
imgdel = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/delete.png")));
imgsmbl = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_blue.png")));
imgsmrd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_red.png")));
imgsmgr = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_green.png")));
imgadd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/add_plus.png")));
imgauto = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation.png")));
imgauto1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation1.png")));
imgavail = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/available.png")));
imgbarc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/barcode.png")));
imgdb = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/db.png")));
imgdoc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/doc.png")));
imgexc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/excel.png")));
imgexc1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/import.png")));
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}catch (Exception e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}
ImageIcon delIcon = new ImageIcon(imgdel);
ImageIcon blIcon = new ImageIcon(imgsmbl);
ImageIcon rdIcon = new ImageIcon(imgsmrd);
ImageIcon grIcon = new ImageIcon(imgsmgr);
ImageIcon addIcon = new ImageIcon(imgadd);
ImageIcon autoIcon = new ImageIcon(imgauto);
ImageIcon autoIcon1 = new ImageIcon(imgauto1);
ImageIcon availIcon = new ImageIcon(imgavail);
ImageIcon barcIcon = new ImageIcon(imgbarc);
ImageIcon dbIcon = new ImageIcon(imgdb);
ImageIcon docIcon = new ImageIcon(imgdoc);
ImageIcon excIcon = new ImageIcon(imgexc);
ImageIcon excIcon1 = new ImageIcon(imgexc1);
Object[] items =
{
noIcon,
delIcon,
blIcon,
rdIcon,
grIcon,
addIcon,
autoIcon,
autoIcon1,
availIcon,
barcIcon,
dbIcon,
docIcon,
excIcon,
excIcon1
};
try {
int iconPosition = Integer.parseInt(FieldProp.getProperty(this.getName() + "Icon"));
String iconProp = FieldProp.getProperty(this.getName() + "Icon");
if (!iconProp.equals("0")) {
this.setIcon((ImageIcon) items[iconPosition]);
}
}catch (Exception e){
e.printStackTrace();
}
try {
this.setEnabled((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Enabled"))));
this.setFocusable((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Focusable"))));
}catch(Exception e){}
Related
I'm trying to read a thumbnail (icon; 32x32px) from a file (.ico/.exe) and set it to a JavaFX label.
My first try:
public Icon getLargeIcon(String exeFile) {
if (exeFile != null) {
File file = new File(exeFile);
try {
ShellFolder sf = ShellFolder.getShellFolder(file);
return new ImageIcon(sf.getIcon(true), sf.getFolderType());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
After that I'm doing this:
Icon largeIcon = getLargeIcon(file.getAbsolutePath());
ImageIcon swingImageIcon = (ImageIcon) largeIcon;
java.awt.Image awtImage = swingImageIcon.getImage();
Image fxImage = javafx.scene.image.Image.impl_fromPlatformImage(awtImage);
lblAppIconValue.setGraphic(new ImageView(fxImage));
I've searched trough several sites and found this, but it gives me an exception:
java.lang.UnsupportedOperationException: unsupported class for loadPlatformImage
My second try:
URL url = file.toURI().toURL();
Image image = new Image(url.toString());
lblAppIconValue.setGraphic(new ImageView(image));
Also not working ...
My question: How can I set a javax.swing.Icon to a JavaFX label? Is it possible? If it's not possible, how can I read a thumbnail from a file and set it as an icon/graphic for a JavaFX label?
Thanks!
Never use impl_ methods: these are not part of the public API.
To convert an awt Image to an FX Image, the SwingFXUtils class in javafx.embed.swing has a toFXImage(...) method that converts a BufferedImage to a JavaFX Image. It's not clear whether the image you have from the icon is a BufferedImage, so you'll need a couple of steps to make that work:
BufferedImage bImg ;
if (awtImage instanceof BufferedImage) {
bImg = (BufferedImage) awtImage ;
} else {
bImg = new BufferedImage(awtImage.getWidth(null), awtImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bImg.createGraphics();
graphics.drawImage(awtImage, 0, 0, null);
graphics.dispose();
}
Image fxImage = SwingFXUtils.toFXImage(bImg, null);
This is a fairly inefficient approach, as you are first creating an awt image from your file, then converting it to an FX image, possibly via an intermediate buffered image. If you have access to the source code for the ShellFolder class, you might see how that implements the getIcon() method and follow the same process. At some point, it must get an InputStream with the image data; once you have that you can pass it to the javafx.scene.image.Image constructor.
If you want to place an image in your application on JavaFX you have 2 main options:
Define it in fxml:
<ImageView>
<Image url="icon.png"/>
</ImageView>
Create Label in your controller:
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
...
Image image = new Image(getClass().getResourceAsStream("icon.png"));
Label label = new Label("Label");
label.setGraphic(new ImageView(image));
icon.png should be placed in the same package with fxml-file or your controller (otherwise you should amend image name in that example).
Update: change image in label dynamically (in accordance with image selected by user).
fxml:
<Button fx:id="setImageButton"/>
<ImageView fx:id="image">
<Image url="defaultImage.png"/>
</ImageView>
Controller:
public class MainController implements Initializable {
public Parent root;
public Button setImageButton;
public ImageView image;
#Override
public void initialize(URL location, ResourceBundle resources) {
setImageButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(root.getScene().getWindow());
if (file != null) {
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image picture = SwingFXUtils.toFXImage(bufferedImage, null);
image.setImage(picture);
} catch (IOException ex) {
// do something
}
}
}
});
}
}
I would like to display image(saved in project folder) in dialog window, but when I run my method showDialogWithImage I get FileNotFoundExcpetion: imgs\pic1.jpg (The system cannot find the file specified), although the image is located there.
I have tried load image on this way too:
Image image = new Image(getClass().getResourceAsStream(path));, but got the same problem.
Are there some others possibilities to load image to ImageView ?
Thank you for help!
My Java code is located in src\myProject\gui in project folder.
path="imgs\pic1.jpg" // imgs is located in project folder
public void showDialogWithImage(String path) {
final Stage dialogStage = new Stage();
logger.info(path);
InputStream is = null;
try {
is = new FileInputStream(path); // here I get FileNotFoundException
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(is);
ImageView view = new ImageView();
view.setImage(image);
Button btnOK = new Button("OK");
btnOK.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
dialogStage.close();
}
});
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create()
.children(view, btnOK).alignment(Pos.CENTER)
.padding(new Insets(35)).build()));
dialogStage.show();
}
getClass().getResourceAsStream(path) will start its file search from the location of the calling class. So by using this path "imgs\pic1.jpg", you're saying this is your file structure
src\myProject\gui\imgs\pic1.jpg
To have the search traverse back, you need the extra separator before imgs. So
"\imgs\pic1.jpg"
Also, I think when you use a back slash as separator, you need to escape it. So
"\\imgs\\pic1.jpg
Or just use forward slash
"/imgs/pic1.jpg
Another option is to use a class loader, that will search from the root, where you don't need the beginning separator
getClass().getClassLoader().getResourceAsStream("imgs/pic1.png");
when you load an image with path you need replace seperator file "\" with "/" for example
String ImageName="MyImage.png";
File file = new File("src\\Messages\\Attachements\\Images");
try {
if (!file.exists()) {
FileUtils.forceMkdir(file);
}
}
} catch (Exception io) {
io.printStackTrace();
}
Image image = new Image("/Messages/Attachements/Images/"+ImageName=);
ImageReceived.setImage(image);
I am writing an image library for fun and i came across a problem that i can't seem to solve. The class is pretty simple: take a picture, process it, display it through JFrame, and finally save it as a BufferedImage (javax.imageio.ImageIO). Here is what my picture looks like through the JFrame (this is my ColorEnhance class... on the Drustan nebula):
Here is what the saved version (a png, but all types ImageIO.write() supports look the same):
I'm not sure where the change occurs, but when I run this through my blur method entire lines appear from nothing in the png... Anyways, here is some code:
public void writeToFile(BufferedImage finalPic, String nameToAppend)
{
String temp=fileName.replace(".", nameToAppend+".");
String ext=fileName.substring(fileName.indexOf(".")+1);
File file=new File(temp);
try
{
ImageIO.write(finalPic, ext.toUpperCase(), file);
System.out.println("Successfully saved to: "+temp);
} catch (Exception e) { e.getMessage(); }
}
public void displayImage(String titleName)
{
ImageIcon icon = new ImageIcon(newPic);
JFrame frame = new JFrame(titleName);
JLabel label = new JLabel(icon);
label.setIcon(icon);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setSize(WIDTH, HEIGHT+22);
frame.setVisible(true);
}
One last thing is that the save works for some processing classes better than others, if you need to see any more code just ask, thanks
Try using PNGImageEncoder from Apache XML Graphics Commons:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
PNGImageEncoder encoder = new PNGImageEncoder(new FileOutputStream("file.png"), null);
encoder.encode((RenderedImage) image);
It seems like i'm not the only one with that question but I can't find an answer that solves the problem.
I created a Label and assign an Icon to it using WYSIWYG interface designer.
Now I want to change the icon dynamically during runtime.
The logic way would be like this (my first attempt) :
ImageIcon newIcon = new ImageIcon("SomePath");
jLabel1.setIcon(newIcon);
When I do this the Icon simply disapears from the interface so I googled it and someone said to "flush" the icon whatever this means I tried it :
ImageIcon newIcon = new ImageIcon("SomePath");
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);
Still having the same problem.. The icon disapears.
What am I doing wrong ?
Update (Full Method) :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
attempted = myEngine.Attempt('q', word);
if(attempted)
{
this.jTextArea1.setText(myEngine.ChangeEncrypt('q', word, this.jTextArea1.getText()));
}
else
{
JOptionPane.showMessageDialog(null,"The Letter Q is not in the word", "Error",JOptionPane.WARNING_MESSAGE);
jButton1.setEnabled(false);
life ++;
ImageIcon newIcon = myEngine.UpdatePicture(life);
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);
}
This is the UpdatePicture Method :
public ImageIcon UpdatePicture(int life)
{
ImageIcon emptyIcon = new ImageIcon();
if (life == 0)
{
ImageIcon iconZero = new ImageIcon("/hang0.gif");
return iconZero;
}
if (life == 1)
{
ImageIcon iconOne = new ImageIcon("/hang1.gif");
return iconOne;
}
if (life == 2)
{
ImageIcon iconTwo = new ImageIcon("/hang2.gif");
return iconTwo;
}
if (life == 3)
{
ImageIcon iconThree = new ImageIcon("/hang3.gif");
return iconThree;
}
if (life == 4)
{
ImageIcon iconFour = new ImageIcon("/hang4.gif");
return iconFour;
}
if (life == 5)
{
ImageIcon iconFive = new ImageIcon("/hang5.gif");
return iconFive;
}
if (life == 6)
{
ImageIcon iconSix = new ImageIcon("/hang6.gif");
return iconSix;
}
return emptyIcon;
}
Not sure the whole code was necessary but still it might help.
The life variable starts at 0.
I checked and in the UpdatePicture it hits the "/hang1.gif"; and returns it.
If the file is in your src folder then :
ImageIcon ii = new ImageIcon(getClass().getResource("/myFile.gif"));
You really should not put that slash before the name of your icon. It works like that.
I know this is an old question thread, but was having a hell of a time getting a icon replaced on a jLabel when my app was running. This is what finally fixed it:
I created a new folder in the source package named images and put the images in there.
In my Frame (main class) I added this below the initComponents method:
private void moreComponents() {
// get images for initial screen prompts (Skd2_startPanel, loading label1).
// StartPanel is a panel on the Frame that has the loading label on it))
try {
lcImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/LC.png")));
clImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/CL.png")));
} catch (IOException ex) {
Logger.getLogger(Skd2_Frame.class.getName()).log(Level.SEVERE, null, ex);
}
}
LC and CL.png are the images I want as the label icons.
In a different class I added the following when I wanted the icon to change:
loadingLabel1.setIcon(lcImage); // or clImage as needed.
You'll need the following imports:
import java.util.logging.Logger;
import javax.imageio.ImageIO;
And add these two declarations just below the variables declarations:
static ImageIcon lcImage;
static ImageIcon clImage;
Hope this helps someone searching the web for an answer.
I have a simple portlet with a few components : 3 Button objects, 1 Slider, 1 MenuBar and a picture assigned to a Label (generated by servlet). Now when I switch between pictures for a Label (I have more of them), I want the picture Label to be placed at the old picture Label object's position:
My picture Label is in the left corner of the portlet. The Button objects, MenuBar, and the Slider are under the picture Label when I select another picture Label the new picture Label is being drawn under the other components (under the Button objects , MenuBar , Slider ) so the Button objects... are top and the picture Label is at the bottom of the portlet
for example, I change the background of the picture Label by selecting the color in the menu :
newItem1.addItem("Blue",new Command(){
public void menuSelected(MenuItem selectedItem){
if(pictureA.isVisible()){
pictureB.setVisible(false);
pictureC.setVisible(false);
window.removeComponent(pictureA);
pictureA= new Label("<img src=http://localhost:8888/portlet/KiviatDiagramm?background=blue", Label.CONTENT_XHTML);
window.addComponent(pictureA);
} else {
window.showNotification("", Notification.TYPE_WARNING_MESSAGE);
}
}
});
UPDATE :
I have switched from Label objects to embedded images (Embedded) (which is a lot better) I have tried to reassign the resource on the Embedded object with the new color but it doesn't work, here is what I've done :
public void init() {
URL PictureAUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureA");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL PictureBUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureB");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL pictureCUrl= null;
try {
pictureCUrl= new URL("http://localhost:8888/portlet/pictureC");
} catch (MalformedURLException e) {
e.printStackTrace();
}
final Embedded pictureA = new Embedded("", new ExternalResource(pictureAURL));
pictureA .setType(Embedded.TYPE_IMAGE);
final Embedded pictureB = new Embedded("", new ExternalResource(pictureBURL));
pictureB .setType(Embedded.TYPE_IMAGE);
final Embedded pictureC = new Embedded("", new ExternalResource(pictureCURL));
pictureC .setType(Embedded.TYPE_IMAGE);
newItem.addItem("ColorBlue", new Command(){
public void menuSelected(MenuItem selectedItem) {
if(!pictureA.equals(pictureB )){
Resource newPictureResource = new ExternalResource("http://localhost:8888/portlet/pictureA?background=blue");
newPictureResource.setType(Embedded.TYPE_IMAGE);
pictureA.setResource(newPictureResource);
}
else {
window.showNotification("Please select pictureA");
}
}
});
rickthomas is correct, you should use the replaceComponent method. I'm pretty sure that the main problem here is that after you have removed the picture, you call addComponent(pictureA) which actually adds the component to the end of the component list. If you don't have a reference to the old picture and it's the first component, then you can use this:
window.replaceComponent(window.getComponentIterator().next(), newPicture);
In addition to that, you don't have to write HTML to show images. You can use Embedded.
If the images are in your classpath, you can use the following:
Embedded newPicture = new Embedded("", new ClassResource("my-picture.png", myApplication));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
If they are found somewhere else, use this:
URL url = new URL("http://localhost:8888/portlet/KiviatDiagramm?background=blue");
Embedded newPicture = new Embedded("", new ExternalResource(url));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
This might solve your problem.
Looking at the Vaadin API javadoc,
I found this
public void replaceComponent(Component oldComponent,Component newComponent)
I haven't tested it... but it should work.