0
I’m trying to insert an image into a HBox
through a ImageView
but I’m not getting it.
Fxml code:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="borderPane" stylesheets="@../css/botoes.css" id="BorderPane" xmlns:fx="http://javafx.com/fxml/1" fx:controller="teste.FXMLDocumentController">
<left>
<HBox fx:id="hbox" alignment="CENTER">
<Button fx:id="btnIniciar" onAction="#acaoIniciar" />
<Button fx:id="btnPausar" onAction="#acaoPausar" />
<Button fx:id="btnParar" onAction="#acaoParar" />
<Button fx:id="btnSubirFila" onAction="#acaoSubirFila" />
<Button fx:id="btnDescerFila" onAction="#acaoDescerFila" />
<Button fx:id="btnRemover" onAction="#acaoRemover" />
<Button fx:id="btnAdicionarArquivo" onAction="#acaoAdicionarArquivo" />
<Button fx:id="btnLinkMagnetico" onAction="#acaoLinkMagnetico" />
<Button fx:id="btnGerarTorrent" onAction="#acaoGerarTorrent" />
</HBox>
</left>
<right>
<HBox fx:id="hbox2" alignment="CENTER">
<ImageView fx:id="image" disable="false" fitHeight="60" fitWidth="60" pickOnBounds="true" preserveRatio="true" />
<TextField fx:id="txtPesquisar" />
</HBox>
</right>
</BorderPane>
To test, I removed the line where I inserted the ImageView
in fxml and added the image via the controller in the following snippet:
this.image = new ImageView(new Image("/imagens/iniciar.png"));
this.image.setFitHeight(TAMANHO_IMAGEM_X);
this.image.setFitWidth(TAMANHO_IMAGEM_Y);
this.hbox2.getChildren().add(image);
And miraculously no error occurred!
I also tried to insert the image into the ImageView
within the fxml file, without creating any object in the controller:
<ImageView fx:id="image" disable="false" fitHeight="60" fitWidth="60" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../imagens/iniciar.png" />
</image>
</ImageView>
But it didn’t work either.
Detail: in this last test appeared, among several others, the following line in the output:
Caused by: javafx.fxml.LoadException: ImageView is not a valid type.
My biggest problems with javafx are like this, I try to use fxml and a controller and start sprouting errors.
Thank you so much for your help! When it comes to observation, I am in a project at school and was chosen to use fxml, so I don’t have much to do. Now, if it’s no bother, allow me to ask you one more question: what would this <Children> tag be and when should I use it?
– Eduardo Toffolo
This tag says that one component is child of another component, sometimes there are properties that are "inherited" from parent components, for example when you mark that all child components use the available space. In the above case, it also serves to indicate who is reading that Imageview is inside Hbox. Do not forget to give ok in the answer that helped you most.
– Gustavo Fragoso
I think I understand.
– Eduardo Toffolo
Thank you very much!!!
– Eduardo Toffolo
As for the ok of the answer, it is given, but my reputation is less than 15, so it is not accounted for.
– Eduardo Toffolo
I forgot to comment that the parent relation > child is between Containers (Anchorpane, Hbox, Vbox, ...) and Controls (Textfield, Button, ...). See how to accept a reply in this post: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply
– Gustavo Fragoso
Thank you again
– Eduardo Toffolo