来自另一个FXML控制器的ImageView的Javafx setImage
我有一个名为“AdjustmentBar”的类,它包括一个imageView、一个菜单栏和一些图片编辑功能(一个FXML文件和一个控制器)。AdjustmentBar被加载到另一个名为“ExamicationDisplayer”的类(也是一个FXML文件和一个控制器)。ExaminationDisplayer从一个在线MySQL数据库加载一些图像,我希望这些图像中的每一个都显示在一个单独的imageView中 我已经能够将AdjustmentBar加载到ExaminationalDisplayer中,但是我不知道如何设置AdjustmentBar中包含的imageView的image()。我可以在检查显示器中专门制作的imageview中显示数据库中的图像,如果单独运行,我可以在AdjustmentBar视图中显示图像。我只是无法让加载的调整栏在检查显示器中显示图像 当我运行它时,我得到了这行代码的nullpointerexception错误:imgView.setImage(image),它位于检查显示器控制器中:来自另一个FXML控制器的ImageView的Javafx setImage,java,javafx,fxml,scenebuilder,Java,Javafx,Fxml,Scenebuilder,我有一个名为“AdjustmentBar”的类,它包括一个imageView、一个菜单栏和一些图片编辑功能(一个FXML文件和一个控制器)。AdjustmentBar被加载到另一个名为“ExamicationDisplayer”的类(也是一个FXML文件和一个控制器)。ExaminationDisplayer从一个在线MySQL数据库加载一些图像,我希望这些图像中的每一个都显示在一个单独的imageView中 我已经能够将AdjustmentBar加载到ExaminationalDisplaye
public class ExaminationDisplayerController extends AdjustmentBarController {
@FXML
private void handlebtnFraminghamAction(ActionEvent event) throws IOException {showCardiacConditionEstimator(); }
@FXML
private AnchorPane anchorPane;
@FXML
private LineChart<Number, Number> lcChart;
@FXML
private NumberAxis xAxis; // x-Axis is defined
@FXML
private NumberAxis yAxis; //y-Axis is defined
public void initialize() throws ClassNotFoundException, SQLException, IOException { // Loading methods in ExaminationDisplayerController
SPECTLoad();
}
public void showCardiacConditionEstimator() throws IOException { // Method for displaying CardiacConditionEstimator
Parent parent = FXMLLoader.load(getClass().getResource("/fxml/CardiacConditionEstimator.fxml")); //Reference to the fxml file
Stage stage = new Stage(); // creating a new stage
Scene scene = new Scene(parent); // creating a new scene for the file to be shown onto
stage.setScene(scene); // sets the scene on the stage
stage.show(); // Displaying the stage
stage.setMaximized(false); // maximizing the stage
}
public void SPECTLoad() throws SQLException, ClassNotFoundException {
DBConnection dbConn = new DBConnection();
Connection conn = dbConn.connect();
PreparedStatement pstmt = null;
try {
for (int numberOfExaminations = 0; numberOfExaminations < 3; numberOfExaminations++) {
String[] ID = {"1111", "1112", "1113"};
List<String> chosenExaminations = Arrays.asList(ID);
String SQL = "SELECT `IMAGE` FROM `SPECT` WHERE `ID` = ?;";
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, chosenExaminations.get(numberOfExaminations));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
InputStream is = rs.getBinaryStream("IMAGE");
OutputStream os = new FileOutputStream(new File("SPECT_IMAGE.jpg"));
byte[] content = new byte[1024];
int size = 0;
while ((size = is.read(content)) != -1) { // Når inputstream er real passer den et -1 værdi og så stoppes loopet
os.write(content, 0, size);
}
os.close();
is.close();
Image image = new Image(new File("SPECT_IMAGE.jpg").toURI().toString(), 200, 200, true, true);
anchorPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/AdjustmentBar.fxml")));
imgView.setImage(image);
}
}
//}
} catch (Exception e) {
e.printStackTrace();
}
}
我不确定AdjustmentBar的FXML文件是否相关,但下面是:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<AnchorPane id="AnchorPane" prefHeight="435.0" prefWidth="435.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Multimodality.controller.AdjustmentBarController">
<children>
<BorderPane prefHeight="435.0" prefWidth="435.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<ScrollPane fx:id="ScrollPane" pannable="true" prefHeight="435.0" prefWidth="435.0" BorderPane.alignment="CENTER">
<content>
<Pane fx:id="imgContainer">
<children>
<ImageView fx:id="imgView" fitHeight="400.0" fitWidth="400.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" />
<Label fx:id="txtLabel" alignment="TOP_LEFT" layoutX="336.0" layoutY="-1.0" prefHeight="134.0" prefWidth="75.0" wrapText="true" />
</children>
</Pane>
</content>
</ScrollPane>
</center>
<bottom>
<HBox alignment="CENTER" prefHeight="35.0" prefWidth="435.0" spacing="8.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnZoomOut" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnZoomOutAction" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/icon_zoomout.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btnZoom100" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnZoom100Action" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/icon_fit.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btnZoomIn" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnZoomInAction" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/icon_zoomin.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btnRotateRight" contentDisplay="CENTER" graphicTextGap="0.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnRotateRightAction" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/shape_rotate_clockwise.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btnRotateLeft" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnRotateLeftAction" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/shape_rotate_anticlockwise.png" />
</image>
</ImageView>
</graphic>
</Button>
<ToggleButton fx:id="btnMeasure" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#btnMeasureAction" prefHeight="25.0" prefWidth="25.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/ruler.png" />
</image>
</ImageView>
</graphic>
</ToggleButton>
<ImageView fitHeight="20.0" fitWidth="20.0" preserveRatio="true">
<image>
<Image url="/img/contrast.png" />
</image>
<HBox.margin>
<Insets left="8.0" right="-2.0" />
</HBox.margin>
</ImageView>
<Slider fx:id="contrastAdjuster" blockIncrement="0.1" majorTickUnit="0.5" max="1.0" min="-1.0" minorTickCount="4" prefHeight="24.0" prefWidth="128.0" showTickMarks="true" />
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
</HBox>
</bottom>
</BorderPane>
</children>
</AnchorPane>
我希望你们中的一些人能帮助我!提前感谢:-)每当您有要求并且需要将控件公开时,这就是代码气味。永远不要暴露你的UI节点 最好的方法是在
AdjustmentBarController
中添加一个方法来接受图像。然后,此方法将在控制器中定义的ImageView中设置此图像
public class AdjustmentBarController {
...
@FXML
private ImageView imgView;
...
public void setImage(Image image) {
imgView.setImage(image);
}
}
// Do not extend from AdjustmentBarController
public class ExaminationDisplayerController {
...
public void SPECTLoad() throws SQLException, ClassNotFoundException {
DBConnection dbConn = new DBConnection();
...
Image image = new Image(new File("SPECT_IMAGE.jpg").toURI().toString(), 200, 200, true, true);
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AdjustmentBar.fxml"));
AnchorPane anchorPaneFromAdjustmentBar = loader.load();
anchorPane.getChildren().add(anchorPaneFromAdjustmentBar);
// Get the controller from the FXMLLoader
AdjustmentBarController controller = (AdjustmentBarController) loader.getController();
// Set the image
controller.setImage(image);
...
}
}
现在,一旦这样做了。不要从AdjustmentBarController
扩展检查显示器控制器
对于在检查显示器控制器
中加载的每个图像,加载调整栏
的FXML文件,从FXMLLoader中取出控制器并在控制器中设置图像
public class AdjustmentBarController {
...
@FXML
private ImageView imgView;
...
public void setImage(Image image) {
imgView.setImage(image);
}
}
// Do not extend from AdjustmentBarController
public class ExaminationDisplayerController {
...
public void SPECTLoad() throws SQLException, ClassNotFoundException {
DBConnection dbConn = new DBConnection();
...
Image image = new Image(new File("SPECT_IMAGE.jpg").toURI().toString(), 200, 200, true, true);
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AdjustmentBar.fxml"));
AnchorPane anchorPaneFromAdjustmentBar = loader.load();
anchorPane.getChildren().add(anchorPaneFromAdjustmentBar);
// Get the controller from the FXMLLoader
AdjustmentBarController controller = (AdjustmentBarController) loader.getController();
// Set the image
controller.setImage(image);
...
}
}
有关更多信息,请浏览:
- (特定于您的用例)
- (一般做法)
有两条语句让我感到困惑——“AdjustmentBar被加载到另一个名为“ExaminationalDisplayer”的类中”和“我已经能够将AdjustmentBar加载到ExaminationalDisplayer中”"? 此外,我可以看到您的检查显示器控制器扩展了AdjustmentBarController,但它似乎不正确。你能解释一下你想在这里实现什么吗?@ItachiUchiha嗨!是的,检查显示器从数据库加载图片,并应在imageView中显示。但是,我不希望它只是一个常规的图像视图,而是应该是我在AdjustmentBar中设计的一个,因为它还有一个工具栏,允许我缩放/旋转/调整每个图像视图的对比度。我无法调用imgView(它是AdjustmentBar的ImageView的fixID),除非我添加了“extend”命令——但这可能根本不对。我希望这是有意义的!我还添加了我在帖子中遇到的错误。:-)@ItachiUchiha也就是说,我有一个名为ExaminationalDisplayer的FXML文件,它加载/打开/显示另一个名为AdjustmentBar的FXML文件。将加载多少AdjustmentBar ImageView取决于一次要显示多少图片。它可能只有一个,但通常是三个或四个。这不是继承的工作方式。继承是类之间的关系。当您执行class AdjustmentBarController{private ImageView imgView;}
时,意味着每个AdjustmentBarController
实例都有一个名为imgView
的字段。当您编写examinationalDisplayerController扩展AdjustmentBarController
时,这意味着每个examinationalDisplayerController
实例也是AdjustmentBarController
的实例(这没有意义),因此,每个检查DisplayerController
实例也有一个imgView
字段。(1/2)但这并不意味着一个实例中的imgView
字段以某种方式设置为与另一个实例中的imgView
字段相同的值。这里有两个控制器:一个是检查显示器控制器
的实例,一个是调整栏控制器
的实例。按照您的方式(同样,这没有意义),每一个都有自己的imgView
字段。仅仅因为该字段在一个实例中被设置为非null值,在另一个实例中并不会以某种方式使其成为非null值。(2/2)谢谢你,这很有帮助!我试着用你的代码从我的项目中加载一个现有的图像文件,效果非常好。但不幸的是,我仍然无法显示SPECT_IMAGE.jpg。我想那一定是因为别的原因,所以我要做一些研究,看看哪里可能是错的。再次感谢你!完成研究后,请随时在下面的评论中提出任何其他问题。@Heidi image具有和属性,请尝试向它们添加侦听器或查询它们以查看是否有图像加载错误。@ItachiUchiha我只需要添加“I”到映像的路径-典型的初学者错误:-)映像映像=新映像(新文件(i+“SPECT_image.jpg”).toURI().toString());