I am having trouble displaying an image using a url or filechooser. I have implemented methods to pull the file from a url or from the pc just not sure how to display those images.
Here is where I call the method that pulls from a url:
@FXML
private void button0Action (ActionEvent event) throws IOException {
catdog c = new catdog();
String cat = c.cat();
System.out.println(cat);
The output dialog returns the url: http://24.media.tumblr.com/tumblr_llmbvhAcsF1qg20muo1_500.gif
Here is my Display class:
package screensaver;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Display extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Scene scene = new Scene(pane);
primaryStage.setTitle("imageViewer");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Created new instance of Display and ImageView:
@FXML
private void button0Action (ActionEvent event) throws IOException {
catdog c = new catdog();
String cat = c.cat();
System.out.println(cat);
Display catDisplay = new Display();
ImageView catImage = new ImageView(cat);
}
How can I then add the image to the pane and call the start method to display the scene? I know I can create an instance of ImageView in the Display class and then add an image to the pane although I can't pull the variable cat which contains the url from the button0Action method. Stuck at the moment and not sure where to go from here. Been struggling for the past few days attempting different methods with no luck. Any ideas?
Here are my changes so far:
Display Class:
package screensaver;
import java.io.IOException;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Display {
public void showImage0 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Catdog c = new Catdog();
pane.getChildren().add(new ImageView(c.cat()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage1 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Catdog d = new Catdog();
pane.getChildren().add(new ImageView(d.dog()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage2 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
LandscapeImage lI = new LandscapeImage();
pane.getChildren().add(new ImageView(lI.imageSource));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage3 (String file) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
FileChooser fc3 = new FileChooser();
pane.getChildren().add(new ImageView(fc3.fileChoice()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
}
Error occurs here: 'void' type not allowed here
pane.getChildren().add(new ImageView(fc3.fileChoice()));
Here is the FileChooser Class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package screensaver;
import java.io.File;
import java.io.IOException;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
/**
*
* @author Jacob
*/
public class FileChooser {
void fileChoice () throws IOException{
javafx.stage.FileChooser fc3 = new javafx.stage.FileChooser();
File selectedFile = fc3.showOpenDialog(null);
fc3.getExtensionFilters().addAll(
new javafx.stage.FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
if (selectedFile != null) {
String location = (selectedFile.getAbsoluteFile().toURI().toString());
System.out.println(location);
Display ownDisplay = new Display();
ownDisplay.showImage3(location);
} else {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION,"No file was selected");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent((ButtonType response) -> {
System.out.println("OK");
});
}
}
}
Newest code:
Display method:
public void showImage3 (String file) throws IOException, Exception {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
pane.getChildren().add(new ImageView(file));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
button3Action method:
@FXML
private void button3Action(ActionEvent event) throws IOException, Exception {
FileChooser fc3 = new FileChooser();
File selectedFile = fc3.showOpenDialog(null);
fc3.getExtensionFilters().addAll(
new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
if (selectedFile != null) {
String location = (selectedFile.getAbsoluteFile().toURI().toString());
System.out.println(location);
Display fileChoice = new Display();
fileChoice.showImage3(location);
} else {
Alert alert = new Alert(AlertType.CONFIRMATION,"No file was selected");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent((ButtonType response) -> {
System.out.println("OK");
});
}
}