我正在制作一个显示滑雪胜地天气数据的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>
答案 0 :(得分:0)
我尝试复制您的代码并进行一些更改以找到错误,我发现您的错误在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 stackoverflow;
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);
将标签添加到每个标签页中,您的代码会将标签添加到选项卡标注的每个内容中。