问题
I am doing an application which user by clicking on an image(ImageView1) can see that in another ImageView2. so I try to get the image of ImageView1 in a variable
BufferedImage img= SwingFXUtils.fromFXImage(ImageView1.getImage(), null);
And then assign that variable to ImageView2
ImageView2.setImage(SwingFXUtils.toFXImage(img, null));
But it seems however, setImage is done successfully, but ImageView2 is not showing anything. anyone can help me for a better solution?
Here is the code example: Controller. ImageView1
@FXML
private void HandleMousePressedOnImageOne()
{
BufferedImage img= SwingFXUtils.fromFXImage(ImageOne.getImage(), null);
try
{
ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
imgSelection.SetBufferedImageOne(img);
FXMLLoader loader= new FXMLLoader();
SplitPane p= loader.load(getClass().getResource("ImageSelection.fxml").openStream());
ImageSelectionController imgSelectionController= (ImageSelectionController)loader.getController();
imgSelectionController.HandleImageOne();
System.out.println("Image One has been Pressed!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
Model.ImageSelection
public class ImageSelection {
private BufferedImage bufferedImageOne;
private static ImageSelection imageSelectionInstace= new ImageSelection();
private ImageSelection(){}
public static ImageSelection getImageSelectionInstance()
{
return imageSelectionInstace;
}
public void SetBufferedImageOne(BufferedImage img)
{
this.bufferedImageOne= img;
}
public BufferedImage getBufferedImageOne()
{
return this.bufferedImageOne;
}
}
Controller2.ImageView2
public void HandleImageOne(){
ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
BufferedImage img= imgSelection.getBufferedImageOne();
ImageOne.setImage(SwingFXUtils.toFXImage(img, null));
}
回答1:
Your code doesn't work because you are loading a new copy of the UI defined in the FXML file ImageSelection.fxml. You don't display this new copy. When you retrieve the controller from the loader that loads that copy of the UI and call
imgSelectionController.HandleImageOne();
you change the image in the ImageView that is part of that new instance of the UI. Since that instance is not displayed, you don't see any effect from that call.
A better approach, which also avoids your first controller having a dependency on the second controller as in your code, is to create an ObjectProperty<Image> in your model, and observe it from the second controller:
public class ImageSelection {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>();
private static ImageSelection imageSelectionInstance= new ImageSelection();
private ImageSelection(){}
public static ImageSelection getImageSelectionInstance() {
return imageSelectionInstance;
}
public ObjectProperty<Image> imageProperty() {
return image ;
}
public final void setImage(Image image) {
imageProperty().set(image);
}
public final Image getImage()
{
return imageProperty().get();
}
}
and in the second controller, do
public class ImageSelectionController {
@FXML
private ImageView imageOne ;
public void initialize() {
ImageSelection.getImageSelectionInstance().imageProperty()
.addListener((obs, oldImage, newImage) -> imageOne.setImage(newImage));
}
// ...
}
Now all the first controller needs to do is set the image in the model:
@FXML
private void handleMousePressedOnImageOne() {
ImageSelection.getImageSelectionInstance().setImage(imageOne.getImage());
}
Note that there's absolutely no need to convert from a JavaFX image to a BufferedImage and back.
I also recommend not using a singleton pattern, for a number of (well-documented; just google "what is wrong with using singletons") reasons. Create a single instance and pass it to each of the controllers, or use a dependency injection framework to manage that for you.
来源:https://stackoverflow.com/questions/45288878/javafx-get-image-of-one-imageview-and-assign-it-to-another-imageview