-1

Below here is my example code. It loads images saved in a local folder by using the remaining image relative path stored in mysql, but it said unknown source..please help..


public void LoadTimelineImg() throws SQLException {
        
    String sql = "select id, img_url from t_timeline order by id";
        
    Statement selecStatement = conn.createStatement();
        
    ResultSet resultSet = selecStatement.executeQuery(sql);
        
    while(resultSet.next()) {
        int id = resultSet.getInt("id");
        ArrayList<Image> images = new ArrayList<Image>();
        String img_path = resultSet.getString("img_url");
        images.add(new Image("\"" + "/application/images/" + img_path + "\""));

            
        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
            imageView.setImage(images.get(count));
            count++;
            if(count == 5){
                count = 0;
                }
            }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

    }
}

howie
  • 2,587
  • 3
  • 27
  • 43
  • Why are you creating multiply `Timelines`? – SedJ601 Jul 19 '20 at 00:48
  • yes sir, i try to create an image transition..those images is in a folder somewhere in my desktop it loads by image path stored in mysql database.. – Jhun Sardia Jul 20 '20 at 04:53
  • I don't know if I understand your reply. For every result in your result set, your code is creating a `Timeline`. The `Timeline` appears to try to change the images every five seconds. That means if you code returns ten images, you will have ten `Timelines`. All of them will be trying to change the `ImageView` every time they individually meet their five seconds quota. This does not seem logical to me. – SedJ601 Jul 20 '20 at 06:27
  • any code suggestion sir?, i have 5 images path ex."application/images/img1.jpg" etc.. stored in mysql..i want to load those images path from mysql in javafx to create animation changes image every 5 sec.. – Jhun Sardia Jul 20 '20 at 12:35
  • The code suggestion is that you remove the `Timeline` from the loop. I would suggest you have a `Database Handler` to only handle database operations. You have database operations mixed with the GUI code. [Here](https://github.com/sedj601/SQLitePersonTableViewExample) is an idea of how to implement a database handler. – SedJ601 Jul 20 '20 at 14:58
  • https://stackoverflow.com/questions/23440980/how-show-specific-part-of-an-image-in-javafx – SedJ601 Jul 21 '20 at 16:44

1 Answers1

0

The Image class constructor take a string as parameter. This string musn't be double quoted like the one you're providing.

So you can do the following:

String rootPath = "/application/images/"; // or "C:/application/images/" on windows
// assuming img_path = "somefolder1/somefolder2/imagename.ext"
Image img = new Image(rootPath + img_path);

or

String rootPath = "/application/images/"; // or "C:/application/images/" on windows
// assuming img_path = "somefolder1/somefolder2/imagename.ext"
File file = new File(rootPath, img_path);
Image img = new Image(file.toURI().toURL().toString());
Mumrah81
  • 2,034
  • 2
  • 16
  • 23