1

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");
        });

    }
    }
user3440443
  • 13
  • 2
  • 8
  • You have to add your **`catImage`** to your (Grid-) **`pane`**. **`pane.getChildren().add(...)`**, respecting the layout. Beside that, it hurts my eyes, when I see variables starting with a capital letter and classes with lower case letter (in Java) – Ben Sep 28 '16 at 07:14
  • Got it to work with urls but am now having trouble with filechooser. – user3440443 Sep 28 '16 at 09:02
  • I dont' really understand your question. Maybe you could add an **Constructor** to your Display-Class which takes the cat object/variable? (But your Display-Class extends Application?!) Do you need just need a dialog? – Ben Sep 28 '16 at 09:08
  • Maybe this can help you: http://stackoverflow.com/a/22167142/3887073 ? – Ben Sep 28 '16 at 09:12
  • I already have a gui. I modified the display class according to fabians answer which is no longer here for some odd reason and was able to add the images to the pane for my urls and it displays correctly. I have another method that uses javafx filechooser to choose an image using file explorer. I am having trouble adding that file to the pane. Error says void type not allowed. fabian posted some code addressing this issue but never got to use it before the answer disappeared. How would I post new code so you can see exactly what I am talking about? – user3440443 Sep 28 '16 at 10:14
  • **Edit** your question and add all your infos (and changed code) to it. – Ben Sep 28 '16 at 10:21
  • Added new Edits – user3440443 Sep 28 '16 at 10:37

1 Answers1

1

Edit 2:
Cause I couldn't really follow you anymore, try to understand this example (it's fully working).
This should help to solve your problem:

public class TestClass extends javafx.application.Application
{
   @Override
   public void start(javafx.stage.Stage primaryStage) 
   {
      javafx.scene.control.Button button = new javafx.scene.control.Button("click me");
      button.setOnAction((javafx.event.ActionEvent event) -> { chooseImageAndDisplay(); });

      javafx.scene.layout.BorderPane pane = new javafx.scene.layout.BorderPane();
      pane.setCenter(button);
      pane.setPrefSize(300, 200);
      javafx.scene.Scene scene = new javafx.scene.Scene(pane);
      primaryStage.setTitle("imageViewer"); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
   }

   private void chooseImageAndDisplay() 
   {
      javafx.stage.FileChooser fc = new javafx.stage.FileChooser();
      fc.getExtensionFilters().addAll(new javafx.stage.FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));

      java.io.File selectedFile = fc.showOpenDialog(null);

      if (selectedFile != null) 
      {
         String location = (selectedFile.getAbsoluteFile().toURI().toString());

         javafx.scene.layout.BorderPane pane = new javafx.scene.layout.BorderPane();
         pane.setPrefSize(300, 200);
         javafx.scene.image.ImageView iw = new javafx.scene.image.ImageView(location);
         pane.setCenter(iw);

         javafx.scene.Scene scene = new javafx.scene.Scene(pane);
         javafx.stage.Stage stage = new javafx.stage.Stage();
         stage.setTitle("imageViewer"); 
         stage.setScene(scene); 
         stage.show(); 
      }
   }

   public static void main(String[] args)
   {
      launch(args);
   }
}
Ben
  • 3,378
  • 30
  • 46
  • I ended up not needing to change my code for filechooser. I just made some slight modification to my display method and my button3ActionEvent. Added my new code to the question. – user3440443 Sep 28 '16 at 15:52
  • Please update your whole question properly according to your new situation/code. I can't follow you anymore. What is the current problem? Is the image not shown? Is the new stage now shown? Any exception? Maybe the image is not shown, because you added it in wrong way. Check how to use GridLayout. You could just replace it with **BorderPane** and call **setCenter(...)** instead of **add(...)** – Ben Sep 28 '16 at 19:41
  • The program works how it is supposed to. No problems with the code I posted. I meant to say that I did not need change my method to reflect how you had it before in order for it to work. I was able to just pass the variable file in showImage3 and call that from my original button action method instead of trying to create a separate class for a file chooser. – user3440443 Sep 29 '16 at 06:49