JAVAFX FXML:动态创建的选项卡中的动态 ImageView 不显示

JAVAFX FXML: dynamic ImageView in dynamically created tabs not displaying

我正在制作一个 java 应用程序来显示滑雪胜地的天气数据。用户登录后,它会创建一个场景,该场景有一个带有动态创建的选项卡的选项卡窗格,每个用户最喜欢的度假村都有一个。所有数据都存储在 mysql 数据库中。

我能够为每个用户成功创建所有选项卡,现在我正在尝试使用 ImageView 对象将与每个度假村关联的图像加载到正在加载的每个选项卡中。我不知道为什么,但我无法在任何选项卡中显示任何图像。理想情况下,我想获取存储在数据库中的图像文件的名称,创建文件路径字符串并加载图像。我也尝试过对文件路径进行硬编码,并在场景生成器中设置图像,但都不起作用。

我最喜欢的控制器,它有 tabPane 并动态创建选项卡

@FXML
private TabPane tabPane;
private Tab myTab;

/**
 * Initializes the controller class.
 * @param url
 * @param rb
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    try {
        DBConnect dbConnection = new DBConnect();
        // GET USER ID, THEN ARRAY OF THE ID'S FAVORITE RESORTS
        int id = LoggedInUser.id;
        //string array of the names of the users favorited resorts
        String[] resorts = dbConnection.getFavorites(id);

        // DYNAMICALLY CREATE TABS BASED ON THE NUMBER OF FAVORITES FOR THE ACTIVE USER.
        for(int i = 0; i < resorts.length; i++) {
            FXMLLoader loader = new FXMLLoader();
            ResortTab newTabController = new ResortTab(resorts[i]);
            loader.setLocation(getClass().getResource("ResortTab.fxml"));
            loader.setController(newTabController);

            Parent parent = loader.load();
            myTab = new Tab(resorts[i]);
            myTab.setContent(parent); 
            tabPane.getTabs().add(myTab);
        }

    } catch (SQLException | ClassNotFoundException | IOException ex) {
        Logger.getLogger(FavoritesController.class.getName()).log(Level.SEVERE, null, ex);
    }

}    

ResortTab,动态创建选项卡的控制器

public class ResortTab implements Initializable{
String name;
DBConnect dbConnection;
private Image image;
private String resort;


@FXML
private Label label;
@FXML
private ImageView logo;

// CONSTRUCTOR
public ResortTab(String name) throws SQLException, ClassNotFoundException {
    resort = name;
    this.name = name;
    // GET LOGO FILE PATH FROM DATABASE AND LOAD IT TO THE SCENE
    dbConnection = new DBConnect();
    //returns file path thats stored in the database example: images/Logos/resortname.jpg
    String path = dbConnection.getLogoPath(name); 

    System.out.println("'src/jesnwdskiweather/" + path + "'");
    System.out.println("Working Directory = " + System.getProperty("user.dir"));

    // Create full path 
    String full_path = "file:'/src/jesnwdskiweather/" + path + "'";

    // String used to test by hardcoding
    String testPath = "file:'src/jesnwdskiweather/images/Logos/RevelstokeMountain.png'";

    System.out.println(full_path); //prints correct file path
    image = new Image(full_path);
    if(image == null){
        System.out.println("Image is null"); //does not print
    }

}

@Override
public void initialize(URL location, ResourceBundle resources) {
    logo.setImage(image);
    logo.setCache(true);
    label.setText(name);
}

ResortTab.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <children>
     <Label fx:id="label" layoutX="342.0" layoutY="112.0" text="Label" />
     <ImageView fx:id="logo" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
  </children>
</AnchorPane>

我尝试复制您的代码并进行一些更改以查找错误,我发现您的错误就在这里 String full_path = "file:'/src/jesnwdskiweather/" + path + "'"; 并且您不需要在路径中添加 /src/ 查看说明

错误路径:

真实路径:

尝试将完整路径更改为 String full_path = "jesnwdskiweather/" + path";

源代码有一些改动:

/*
 * 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 Whosebug;

import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 * FXML Controller class
 *
 * @author Electron
 */
public class ResortTab implements Initializable {

    String name;
    private Image image;
    private String resort;
    @FXML
    private Label label;
    @FXML
    private ImageView logo;

// CONSTRUCTOR
    public ResortTab(String name) throws SQLException, ClassNotFoundException {
        resort = name;
        this.name = name;
        // GET LOGO FILE PATH FROM DATABASE AND LOAD IT TO THE SCENE
      //  dbConnection = new DBConnect();
        //returns file path thats stored in the database example: images/Logos/resortname.jpg
        String path = "1487868084_39_Email_Marketing.png";

        System.out.println("'src/jesnwdskiweather/" + path + "'");
        System.out.println("Working Directory = " + System.getProperty("user.dir"));

        // Create full path 
        String full_path = "jesnwdskiweather/" + path +"";

        // String used to test by hardcoding
        String testPath = "file:'src/jesnwdskiweather/images/Logos/RevelstokeMountain.png'";

        System.out.println(full_path); //prints correct file path
        image = new Image(full_path);
        if (image == null) {
            System.out.println("Image is null"); //does not print
        }

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        logo.setImage(image);
        logo.setCache(true);
        label.setText(name);
    }
}

注意:您的代码将图像添加到选项卡注释的每个内容中,如果您需要将图像添加到每个选项卡中,请使用 setGraphic(node);.