JavaFx: jfxTextField with icons - javafx

Hi I'm not able to add an icon to my jfxtextfield and found nothing about
could anyone help?
fxml:
<VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox fx:id="boxLogo" alignment="CENTER" prefHeight="100.0" prefWidth="300.0">
<children>
<FontAwesomeIconView glyphName="HOME" size="50">
<fill>
<LinearGradient endX="1.0" endY="1.0" startY="0.49047619047619045">
<stops>
<Stop color="#0d96ff" />
<Stop color="#010f1b" offset="1.0" />
</stops>
</LinearGradient>
</fill>
</FontAwesomeIconView>
</children>
</HBox>
<GridPane fx:id="boxField" alignment="CENTER" prefHeight="222.0" prefWidth="300.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="131.0" minHeight="10.0" prefHeight="108.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="142.0" minHeight="10.0" prefHeight="107.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="142.0" minHeight="10.0" prefHeight="107.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXTextField fx:id="userTxt" alignment="CENTER" focusColor="#1cffb3" labelFloat="true" maxWidth="-Infinity" prefWidth="200.0" text="Digite o seu Login" unFocusColor="#00fffb" GridPane.halignment="CENTER" />
<JFXTextField alignment="CENTER" focusColor="#00ff77" maxWidth="-Infinity" prefWidth="200.0" text="Digite a sua senha" unFocusColor="#00fffb" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<JFXCheckBox text="Lembrar Usuario" GridPane.halignment="RIGHT" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="50.0" />
</GridPane.margin>
</JFXCheckBox>
</children>
</GridPane>
<HBox fx:id="boxButtons" alignment="TOP_CENTER" prefHeight="100.0" prefWidth="300.0" spacing="30.0">
<children>
<JFXButton buttonType="RAISED" prefHeight="25.0" prefWidth="86.0" ripplerFill="#09fff7" style="-fx-background-color: #0DA5E2;" text="Entrar" textFill="WHITE" textOverrun="CLIP">
<font>
<Font name="Arial" size="12.0" />
</font>
</JFXButton>
</children>
</HBox>
</children>
</VBox>
my controller:
public class MainController {
#FXML
private VBox root;
#FXML
private HBox boxLogo;
#FXML
private GridPane boxField;
#FXML
private HBox boxButtons;
#FXML
private JFXTextField userTxt;
public void initialize() {
FontAwesomeIconView icon = new FontAwesomeIconView(FontAwesomeIcon.USER);
}
}
I wanted to add the icon I created to my textfiedl userTxt
I don't know if this can be done by controller or just by css?
From what I found is through css, but I would need to add a folder with a photo to work I wanted to do that with an icon is possible?

I propose to add this in your controller :
myJFXTextField.setstyle("-fx-background
image:url('http://icons.iconarchive.com/icons/rockettheme/halloween/32/pumpkin-
icon.png');");
and this in your css
.text-field {
-fx-background-repeat: no-repeat;
-fx-background-position: right center;
-fx-font-size: 20;
}
You can use a path or URL

Related

I cant parse text from the JavaFX TextArea and TextField [duplicate]

This question already has answers here:
Canvas pane doesn't refreshing(JavaFX) [closed]
(1 answer)
Is #FXML needed for every declaration?
(3 answers)
Closed 7 days ago.
I am using the JavaFX as my GUI and JDBC as my API for database. The method saveCard() is supposed to parse text from the JavaFX TextArea and TextField which then saves the data through the database.insertQuestionAndAnswer() method. It is not and only returning an empty string.
Java code:
package workspace;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class CardMakerController {
#FXML
private CardDatabase database = new CardDatabase();
private TextField questionContainer = new TextField();
private TextArea answerContainer = new TextArea();
public void changeViewToDefaultScreen(ActionEvent e) throws IOException {
App.setRootForScene("welcomeScreen");
}
public void saveCard() {
String cardQuestion = questionContainer.getText();
String cardAnswer = answerContainer.getText();
System.out.println("cardQuestion" + cardAnswer);
database.insertQuestionAndAnswer(cardQuestion, cardAnswer);
}
}
FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane prefHeight="373.0" prefWidth="579.0" style="-fx-background-color: khaki; -fx-border-color: orange;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="workspace.CardMakerController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="CREATE FLASH CARD" textAlignment="CENTER" wrapText="true" GridPane.columnIndex="2">
<font>
<Font name="Cascadia Code Regular" size="12.0" />
</font>
</Label>
<Label text="QUESTION:" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="TOP">
<font>
<Font name="Cascadia Code Regular" size="12.0" />
</font>
</Label>
<Label text="ANSWER/S:" GridPane.columnIndex="1" GridPane.rowIndex="2" GridPane.valignment="BOTTOM">
<font>
<Font name="Cascadia Code Regular" size="12.0" />
</font>
<GridPane.margin>
<Insets bottom="12.0" />
</GridPane.margin>
</Label>
<TextField fx:id="questionContainer" prefHeight="25.0" prefWidth="125.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="1" GridPane.valignment="BOTTOM" />
<TextArea fx:id="answerContainer" prefHeight="70.0" prefWidth="348.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="3" GridPane.rowSpan="2" />
<DialogPane style="-fx-background-color: KHAKI;" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="5" />
<Button mnemonicParsing="false" onAction="#saveCard" style="-fx-background-color: orange;" text="Save Card" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER">
<font>
<Font name="Cascadia Code Regular" size="12.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#changeViewToDefaultScreen" style="-fx-background-color: orange;" text="Change View" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER">
<font>
<Font name="Cascadia Code Regular" size="12.0" />
</font>
</Button>
</children>
</GridPane>
I tried to check if the String parsed is null through an if loop to test it, but is not. When I check the database there is no word that is saved. It is empty pleas help

How can I make the JavaFx scene responsive?

public class DoctorsController implements Initializable {
#FXML
private JFXComboBox<String> comboSpecialization;
#FXML
private JFXComboBox<String> comboDepartment;
#FXML
private ToggleGroup q;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// Populate Comboboxe with static options,
comboSpecialization.getItems().addAll("Pychologist","Psychiatric","Gynaecologist"
+"Pathologist","Cardiologist","Orginologist","unspecified");
comboSpecialization.getSelectionModel().selectLast();
comboDepartment.getItems().addAll("Pychology","Maternity","OutPatient"
+ "Mourtuary","Others");
comboDepartment.getSelectionModel().selectLast();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXComboBox?>
<?import com.jfoenix.controls.JFXProgressBar?>
<?import com.jfoenix.controls.JFXRadioButton?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="684.0" prefWidth="1105.0" stylesheets="#../styles/styles.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="doctors.DoctorsController">
<children>
<JFXTabPane layoutX="138.0" layoutY="100.0" prefHeight="684.0" prefWidth="1105.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="MANAGE DOCOTRS">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<HBox layoutX="23.0" layoutY="33.0" prefHeight="135.0" prefWidth="1068.0" spacing="30.0">
<children>
<AnchorPane prefHeight="110.0" prefWidth="393.0" style="-fx-background-color: #00ADD8;" styleClass="card-unpadded">
<children>
<Pane layoutX="-1.0" prefHeight="141.0" prefWidth="23.0" style="-fx-background-color: #007390;" />
<ImageView fitHeight="97.0" fitWidth="124.0" layoutX="33.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../imgs/doctor.png" />
</image>
</ImageView>
<Label layoutX="182.0" layoutY="14.0" prefHeight="67.0" prefWidth="89.0" styleClass="custom-white-title-label" text="16" />
<Label layoutX="182.0" layoutY="76.0" prefHeight="31.0" prefWidth="210.0" styleClass="custom-white-label" text="Registered doctors" />
<Label layoutX="182.0" layoutY="107.0" prefHeight="33.0" prefWidth="210.0" styleClass="custom-white-label" text="We need you." />
</children>
</AnchorPane>
<AnchorPane prefHeight="166.0" prefWidth="393.0" style="-fx-background-color: #DD4B39;" styleClass="card-unpadded">
<children>
<Pane layoutX="-1.0" prefHeight="166.0" prefWidth="23.0" style="-fx-background-color: #C74433;" />
<ImageView fitHeight="125.0" fitWidth="124.0" layoutX="33.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../imgs/Appointment.png" />
</image>
</ImageView>
<Label layoutX="209.0" layoutY="14.0" prefHeight="67.0" prefWidth="89.0" styleClass="custom-white-title-label" text="57 of 100 " />
<Label layoutX="182.0" layoutY="76.0" prefHeight="31.0" prefWidth="210.0" styleClass="custom-white-label" text="Unattended appointments" />
<JFXProgressBar layoutX="179.0" layoutY="120.0" progress="0.52" />
</children>
</AnchorPane>
<AnchorPane prefHeight="157.0" prefWidth="205.0" style="-fx-background-color: #F39C12;" styleClass="card">
<children>
<VBox layoutX="14.0" layoutY="14.0" prefHeight="85.0" prefWidth="159.0">
<children>
<Label prefHeight="31.0" prefWidth="153.0" styleClass="custom-white-title-label" text="TODAY" />
<Label prefHeight="20.0" prefWidth="152.0" styleClass="custom-white-title-label" text="Monday" />
<Label prefHeight="20.0" prefWidth="151.0" styleClass="custom-white-title-label" text="12/07/2017" />
</children>
</VBox>
<ImageView fitHeight="31.0" fitWidth="50.0" layoutX="143.0" layoutY="102.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../imgs/Copyright.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</children>
</HBox>
<AnchorPane layoutX="23.0" layoutY="224.0" prefHeight="413.0" prefWidth="1057.0" styleClass="card-unpadded">
<children>
<VBox layoutX="20.0" layoutY="14.0" prefHeight="380.0" prefWidth="415.0" spacing="10.0">
<children>
<JFXTextField focusColor="#00add8" labelFloat="true" promptText="First Name" unFocusColor="#868282" />
<JFXTextField focusColor="#00add8" labelFloat="true" layoutX="10.0" layoutY="10.0" promptText="Last Name" unFocusColor="#868282" />
<JFXTextField focusColor="#00add8" labelFloat="true" layoutX="10.0" layoutY="46.0" promptText="Email address" unFocusColor="#868282" />
<JFXTextField focusColor="#00add8" labelFloat="true" layoutX="10.0" layoutY="82.0" promptText="Phone Number" unFocusColor="#868282" />
<JFXTextField focusColor="#00add8" labelFloat="true" layoutX="10.0" layoutY="118.0" promptText="Identity Number" unFocusColor="#868282" />
<JFXTextArea maxWidth="404.0" minWidth="404.0" prefHeight="98.0" prefWidth="404.0" promptText="Additional Information" />
</children>
<padding>
<Insets left="20.0" top="10.0" />
</padding>
</VBox>
<VBox layoutX="525.0" layoutY="17.0" prefHeight="376.0" prefWidth="511.0" spacing="10.0">
<children>
<VBox prefHeight="82.0" prefWidth="541.0">
<children>
<Label style="-fx-text-fill: #868282;" text="Level of Specialization" />
<JFXComboBox fx:id="comboSpecialization" prefHeight="40.0" prefWidth="494.0">
<VBox.margin>
<Insets left="100.0" />
</VBox.margin>
</JFXComboBox>
</children>
</VBox>
<VBox layoutX="10.0" layoutY="10.0" prefHeight="82.0" prefWidth="541.0">
<children>
<Label prefHeight="20.0" prefWidth="112.0" style="-fx-text-fill: #868282;" text="Department" />
<JFXComboBox fx:id="comboDepartment" prefHeight="40.0" prefWidth="499.0">
<VBox.margin>
<Insets left="100.0" />
</VBox.margin>
</JFXComboBox>
</children>
</VBox>
<VBox layoutX="10.0" layoutY="92.0" prefHeight="82.0" prefWidth="541.0">
<children>
<Label prefHeight="20.0" prefWidth="222.0" style="-fx-text-fill: #868282;" text="Taking Appointments" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="30.0">
<children>
<JFXRadioButton selected="true" text="YES">
<toggleGroup>
<ToggleGroup fx:id="q" />
</toggleGroup></JFXRadioButton>
<JFXRadioButton layoutX="10.0" layoutY="10.0" text="NO" toggleGroup="$q" />
</children>
<padding>
<Insets left="100.0" right="20.0" />
</padding>
</HBox>
</children>
</VBox>
<Separator prefWidth="200.0" />
<HBox alignment="CENTER_RIGHT" prefHeight="77.0" prefWidth="511.0" spacing="20.0">
<children>
<JFXButton styleClass="cancel-button" text="Cancel" />
<JFXButton prefHeight="27.0" prefWidth="78.0" styleClass="green-btn" text="Edit" />
<JFXButton layoutX="72.0" layoutY="10.0" prefHeight="37.0" prefWidth="100.0" styleClass="blue-btn" text="Save" />
</children>
<padding>
<Insets right="20.0" />
</padding>
</HBox>
</children>
<padding>
<Insets left="20.0" top="20.0" />
</padding>
</VBox>
</children>
</AnchorPane>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="DETAILS">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<SplitPane dividerPositions="0.6173228346456693" layoutX="16.0" layoutY="6.0" orientation="VERTICAL" prefHeight="649.0" prefWidth="1105.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="388.0" prefWidth="1103.0">
<children>
<TextField layoutX="787.0" layoutY="14.0" prefHeight="30.0" prefWidth="302.0" promptText="Search doctor by name" styleClass="search-box" />
<TableView layoutX="11.0" layoutY="53.0" prefHeight="326.0" prefWidth="1080.0">
<columns>
<TableColumn editable="false" prefWidth="389.0" text="C1" />
<TableColumn editable="false" prefWidth="168.0" sortable="false" text="C2" />
<TableColumn editable="false" prefWidth="171.0" sortable="false" text="C2" />
<TableColumn editable="false" prefWidth="250.0" sortable="false" text="C2" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="240.0" prefWidth="1103.0">
<children>
<HBox layoutX="30.0" layoutY="14.0" prefHeight="210.0" prefWidth="1043.0" spacing="30.0">
<children>
<GridPane prefHeight="219.0" prefWidth="493.0" styleClass="card">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="195.0" minWidth="10.0" prefWidth="106.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="321.0" minWidth="10.0" prefWidth="321.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Personal Details" />
<Label prefHeight="20.0" prefWidth="83.0" text="Contacts" GridPane.rowIndex="4" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="blue-label" text="ID No 30229182" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="blue-label" text="Doctor" GridPane.columnIndex="1" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="blue-label" text="Daniel" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="blue-label" text="Mungatana" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="green-label" text="0717160344" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label prefHeight="20.0" prefWidth="300.0" styleClass="green-label" text="danmlayah#gmail.com" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
<GridPane prefHeight="219.0" prefWidth="520.0" styleClass="card">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="239.0" minWidth="10.0" prefWidth="186.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="302.0" minWidth="10.0" prefWidth="302.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Speciality" />
<Label text="Taking Appointments ?" GridPane.rowIndex="2" />
<Label text="Pending Appointments" GridPane.rowIndex="3" />
<Label prefHeight="20.0" prefWidth="280.0" styleClass="yellow-label" text="Cardiologist" GridPane.columnIndex="1" />
<Label prefHeight="20.0" prefWidth="280.0" styleClass="yellow-label" text="Department of Cardiology" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label prefHeight="20.0" prefWidth="279.0" styleClass="red-label" text="YES" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="1" GridPane.rowIndex="3">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="30.0" prefWidth="98.0" style="-fx-font-weight: bold;" styleClass="green-label" text="12" />
<JFXButton buttonType="RAISED" prefHeight="30.0" prefWidth="99.0" style="-fx-background-color: rgb(0, 166, 90); -fx-text-fill: white; -fx-font-size: 14; -fx-font-weight: bold;" text="View" />
</children>
</HBox>
<JFXButton buttonType="RAISED" ripplerFill="#53eb11" styleClass="blue-btn" text="Edit Info" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
</children>
</HBox>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</JFXTabPane>
</children>
</AnchorPane>
I am using scene builder for building the application.
I have added several panes and image views inside a scene right now i have a fixed resolution of 1366*768, which works fine.
But when the resolution is not matched the elements in the scene are not aligned properly , is there any solution to fix this issue.
Application running on 800*600 resolution
Application running on 1024*768 resolution
Appplication running on 1366*768 resolution

JavaFX abstract controller inheritance didn't work

I have a big problem while coding my application.
Well, I try to extend my ReaderController class, which is abstract and it is set as controller in my fxml file.
There is my code:
public abstract class ReaderController extends AnchorPane{
#FXML
TextField id;
#FXML
TextField name;
#FXML
TextField surname;
#FXML
TextField PESEL;
#FXML
TextField dateOfBirth;
#FXML
TextField documentType;
#FXML
TextField documentNumber;
#FXML
TextField email;
#FXML
TextField phoneNumber;
#FXML
TextField street;
#FXML
TextField city;
#FXML
Button addReader;
Parent root;
public ReaderController()
{
}
And there is my subclass:
public class EditReaderController extends ReaderController{
public EditReaderController() throws IOException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AddUser.fxml"));
root= (Parent) loader.load();
loader.setController(this);
Stage stage = new Stage();
stage.setTitle("Edit User");
stage.setScene(new Scene(root));
stage.setResizable(false);
stage.show();
}
public void initialize(){
addReader.setText("Edit!");
id.setText(String.valueOf(SQLController.getInstance().getLastIndexOfReader()));
}
I have an error:
aused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 75 more
So how can I extend my abstract controller into my EditReaderController ?
UPD 1 my fxml file
<?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.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.library.controllers.ReaderController">
<children>
<HBox layoutX="151.0" layoutY="64.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<VBox prefHeight="400.0" prefWidth="129.0">
<children>
<GridPane prefHeight="300.0" prefWidth="129.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Nr czytelnika" />
<Label text="ImiÄ™" GridPane.rowIndex="1" />
<Label text="Nazwisko" GridPane.rowIndex="2" />
<Label text="PESEL" GridPane.rowIndex="3" />
<Label text="Data urodzenia" GridPane.rowIndex="4" />
</children>
<VBox.margin>
<Insets top="15.0" />
</VBox.margin>
</GridPane>
</children>
</VBox>
<VBox prefHeight="400.0" prefWidth="178.0">
<children>
<GridPane prefHeight="300.0" prefWidth="178.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="id" editable="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="26.0" prefWidth="34.0" />
<TextField fx:id="name" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="surname" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="PESEL" GridPane.rowIndex="3">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="dateOfBirth" editable="false" GridPane.rowIndex="4">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
</children>
<VBox.margin>
<Insets top="15.0" />
</VBox.margin>
</GridPane>
</children>
</VBox>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<GridPane prefHeight="300.0" prefWidth="99.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Typ dokumentu" />
<Label text="Nr dokumentu" GridPane.rowIndex="1" />
<Label text="E-mail" GridPane.rowIndex="2" />
<Label text="Nr telefonu" GridPane.rowIndex="3" />
<Label text="Ulica" GridPane.rowIndex="4" />
<Label text="Miasto" GridPane.rowIndex="5" />
</children>
<VBox.margin>
<Insets top="15.0" />
</VBox.margin>
</GridPane>
</children>
</VBox>
<VBox prefHeight="400.0" prefWidth="195.0">
<children>
<GridPane prefHeight="300.0" prefWidth="195.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="documentType">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="documentNumber" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="email" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="phoneNumber" GridPane.rowIndex="3">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="street" GridPane.rowIndex="4">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
<TextField fx:id="city" GridPane.rowIndex="5">
<GridPane.margin>
<Insets right="25.0" />
</GridPane.margin>
</TextField>
</children>
<VBox.margin>
<Insets top="15.0" />
</VBox.margin>
</GridPane>
<Button fx:id="addReader" alignment="CENTER" mnemonicParsing="false" onAction="#onAddReaderAction" text="Dodaj" textAlignment="CENTER">
<VBox.margin>
<Insets left="100.0" top="25.0" />
</VBox.margin>
</Button>
</children>
</VBox>
</children>
</HBox>
</children>
</AnchorPane>
Why are you using an abstract class in your fxml?
Look at this line:
fx:controller="com.library.controllers.ReaderController"
You have a non-abstract subclass. Why not use it?
fx:controller="com.library.controllers.EditReaderController"
Or use your own non-abstract class.
I didn't see any abstract members in ReaderController.
Removing the abstract modifier may also solve this.
Also, it's really confusing that your controller class is extending AnchorPane..
Also, according to #sillyfly in the comments,
there really isn't much use calling setController after the call to FXMLLoader#load, as by this point the fields have been injected... I think it would even raise an exception about the controller already being set...
According to the comment,
I know it, but i didn't know, how to set a new Controller through action ( for example clicked Edit or Add button)
You should first read https://docs.oracle.com/javafx/2/ui_controls/button.htm to know how to run some code when a button is clicked, and run
loader.setController(your new controller);
when your button is clicked, and remove the fx:controller field, because you already set the controller in your logic code.

JavaFX -- controller don't see the button or event

Why, when I click to the "New" button, text are not change?
A Main class:
public class MainApp extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
BorderPane rootLayout = FXMLLoader.load(getClass().getResource("view/RootLayout.fxml"));
AnchorPane code = FXMLLoader.load(getClass().getResource("view/DeskLayout.fxml"));
rootLayout.setCenter(code);
GridPane tool = FXMLLoader.load(getClass().getResource("view/ToolLayout.fxml"));
rootLayout.setBottom(tool);
primaryStage.setTitle("MyLittleIDE");
primaryStage.setScene(new Scene(rootLayout));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
ToolLayoutController:
public class ToolLayoutController {
#FXML
private Button newButton;
#FXML
protected void handleNewProject(ActionEvent event) {
newButton.setText("123");
}
ToolLaout.fxml
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.mrchebik.view.ToolLayoutController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="newButton" mnemonicParsing="false" onAction="#handleNewProject" prefHeight="30.0" prefWidth="150.0" text="New" GridPane.columnIndex="0" />
<Button fx:id="compile" mnemonicParsing="false" prefHeight="51.0" prefWidth="150.0" text="Compile" GridPane.columnIndex="1" />
<Button fx:id="run" mnemonicParsing="false" prefHeight="54.0" prefWidth="150.0" text="Run" GridPane.columnIndex="2" />
<Button fx:id="save" mnemonicParsing="false" prefHeight="59.0" prefWidth="150.0" text="Save" GridPane.columnIndex="3" />
</children>
</GridPane>
RootLaout.fxml:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
DeskLayout.fxml
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.8015075376884422" layoutX="196.0" layoutY="87.0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextArea fx:id="code" layoutX="39.0" layoutY="-27.0" prefHeight="315.0" prefWidth="598.0" promptText="Code" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextArea fx:id="inOutPut" layoutX="-21.0" layoutY="-73.0" prefHeight="75.0" prefWidth="598.0" promptText="Input/Output" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
I also try to make a sample application with javafx, and add toollayout.fxml only, controller and it works. But I can't do this on this case.

resize stage components when resizing stage

The below screen shot is the default size of stage
when resized
how to resize the inner components when resizing the stage window accordingly?
<AnchorPane fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="809.0" prefWidth="860.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxhomeui_1.HomeUI_2Controller">
<children>
<VBox layoutX="695.0" layoutY="137.0" AnchorPane.bottomAnchor="341.0" AnchorPane.leftAnchor="695.0" AnchorPane.rightAnchor="-2.0" AnchorPane.topAnchor="137.0">
<children>
<SplitPane fx:id="sidebar" layoutX="695.0" layoutY="137.0" prefHeight="331.0" prefWidth="167.0" AnchorPane.bottomAnchor="341.0" AnchorPane.leftAnchor="695.0" AnchorPane.rightAnchor="-2.0" AnchorPane.topAnchor="137.0" />
</children>
</VBox>
<HBox layoutX="37.0" layoutY="163.0" AnchorPane.bottomAnchor="38.0" AnchorPane.leftAnchor="37.0" AnchorPane.rightAnchor="257.0" AnchorPane.topAnchor="163.0">
<children>
<ScrollPane fx:id="displayscroll" layoutX="37.0" layoutY="163.0" prefHeight="608.0" prefWidth="566.0" AnchorPane.bottomAnchor="29.0" AnchorPane.leftAnchor="37.0" AnchorPane.rightAnchor="197.0" AnchorPane.topAnchor="163.0">
<content>
<GridPane fx:id="gridpane" prefHeight="383.0" prefWidth="449.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</content>
</ScrollPane>
</children>
</HBox>
<Pane layoutX="573.0" layoutY="46.0" AnchorPane.bottomAnchor="738.0" AnchorPane.leftAnchor="573.0" AnchorPane.rightAnchor="107.0" AnchorPane.topAnchor="46.0">
<children>
<ComboBox fx:id="categorycmb" prefHeight="25.0" prefWidth="180.0" />
</children>
</Pane>
<Pane layoutX="110.0" layoutY="108.0" AnchorPane.bottomAnchor="672.0" AnchorPane.leftAnchor="110.0" AnchorPane.rightAnchor="461.0" AnchorPane.topAnchor="108.0">
<children>
<ToggleButton fx:id="gamestbutton" layoutX="76.0" layoutY="4.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" stylesheets="#Togglebutton.css" text="Games" AnchorPane.topAnchor="112.0" />
<ToggleButton fx:id="songstbutton" layoutX="218.0" layoutY="4.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" stylesheets="#Togglebutton.css" text="Songs" AnchorPane.topAnchor="112.0" />
<ToggleButton fx:id="apptbutton" layoutX="5.0" layoutY="4.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" stylesheets="#Togglebutton.css" text="Apps" AnchorPane.topAnchor="112.0" />
<ToggleButton fx:id="moviestbutton" layoutX="147.0" layoutY="4.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" stylesheets="#Togglebutton.css" text="Movies" AnchorPane.topAnchor="112.0" />
</children>
</Pane>
<Pane layoutX="92.0" layoutY="44.0" AnchorPane.bottomAnchor="737.0" AnchorPane.leftAnchor="92.0" AnchorPane.rightAnchor="347.0" AnchorPane.topAnchor="44.0">
<children>
<TextField fx:id="searchtxt" prefHeight="28.0" prefWidth="421.0" promptText="Search" AnchorPane.bottomAnchor="737.0" AnchorPane.leftAnchor="92.0" AnchorPane.rightAnchor="347.0" AnchorPane.topAnchor="44.0" />
</children>
</Pane>
</children>
</AnchorPane>
FXML code (above) scrollpane and splitpane enclosed inside HBox and VBox respectively.
Also I want to keep space between images..

Resources