Issue
i am making a simple library application and while building the login screen i have the following 3 problems.
[the login window][1] [1]: https://i.stack.imgur.com/ZB7jJ.png
problem 1: while launching the application i cannot type in the textfields, but i can highlight them.
problem 2: the prompttext given for my 2 textfields is not showing (should be Username and Password)
problem 3: my button is not clickable while it is not disabled in properties
here is my current code.
the view
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="489.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
<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>
<children>
<Label text="Login" textFill="#a49f9f">
<padding>
<Insets bottom="100.0" left="100.0" right="100.0" top="100.0" />
</padding>
<font>
<Font size="38.0" />
</font>
</Label>
<Button fx:id="LoginBtn" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="28.0" prefWidth="133.0" style="-fx-background-color: #0078D7; -fx-background-radius: 0;" text="Login" textFill="WHITE" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
</Button>
<TextField fx:id="usernametxt" layoutX="110.0" layoutY="248.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="274.0" promptText="Username" GridPane.rowIndex="1">
<padding>
<Insets bottom="50.0" left="50.0" right="50.0" top="50.0" />
</padding>
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
<font>
<Font name="Arial" size="12.0" />
</font>
</TextField>
<TextField fx:id="passwordtxt" layoutX="10.0" layoutY="148.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="274.0" promptText="Password" GridPane.hgrow="NEVER" GridPane.rowIndex="2" GridPane.vgrow="NEVER">
<padding>
<Insets bottom="50.0" left="50.0" right="50.0" top="50.0" />
</padding>
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
Controller:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class HelloController {
@FXML
private Button LoginBtn; // yes this is the same as my fxid
public void onLoginBtnClick() {
LoginBtn.setDisable(true);
}
}
finally the application itself
package com.inholland.nl.eindopdracht;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 489, 400);
stage.setTitle("Login");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Solution
Problems 1 and 2 are caused by the excessive padding in the text fields, which doesn't allow any space to type any text. Remove the padding.
Problem 3 doesn't appear for me. Note that you have not specified a controller in the FXML, so no code is actually executed. Add fx:controller="com.inholland.nl.eindopdracht.HelloController"
to the GridPane
element.
Note also you should not specify layout coordinates when using a layout pane.
The following FXML works:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="489.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.inholland.nl.eindopdracht.HelloController">
<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>
<children>
<Label text="Login" textFill="#a49f9f">
<padding>
<Insets bottom="100.0" left="100.0" right="100.0" top="100.0" />
</padding>
<font>
<Font size="38.0" />
</font>
</Label>
<Button fx:id="LoginBtn" onAction="#onLoginBtnClick" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="28.0" prefWidth="133.0" style="-fx-background-color: #0078D7; -fx-background-radius: 0;" text="Login" textFill="WHITE" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
</Button>
<TextField fx:id="usernametxt" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="274.0" promptText="Username" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
<font>
<Font name="Arial" size="12.0" />
</font>
</TextField>
<TextField fx:id="passwordtxt" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="274.0" promptText="Password" GridPane.hgrow="NEVER" GridPane.rowIndex="2" GridPane.vgrow="NEVER">
<GridPane.margin>
<Insets bottom="50.0" left="100.0" right="50.0" top="50.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
Answered By - James_D
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)