In my implementation, I utilize the onKeyPressed event to invoke the letterTyped function within the Pane situated at the top of the FXML file. Whenever a key is typed, my expectation is to have the keyCode printed, demonstrating that the method is successfully being called and executed as intended.
Driver class
public class Driver extends Application {
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("GameInterface.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
catch (IOException e) {
System.out.println("Could not load LoginScreen.fxml");
}
}
}
FXML class
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#letterTyped" prefHeight="675.0" prefWidth="520.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="UserUI">
<children>
<HBox layoutX="119.0" layoutY="43.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R1" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R1" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R1" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R1" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R1" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children></HBox>
<HBox layoutX="119.0" layoutY="113.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R2" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R2" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R2" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R2" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R2" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children>
</HBox>
<HBox layoutX="119.0" layoutY="184.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R3" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R3" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R3" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R3" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R3" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children>
</HBox>
<HBox layoutX="119.0" layoutY="253.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R4" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R4" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R4" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R4" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R4" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children>
</HBox>
<HBox layoutX="119.0" layoutY="324.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R5" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R5" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R5" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R5" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R5" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children>
</HBox>
<HBox layoutX="119.0" layoutY="394.0" prefHeight="62.0" prefWidth="275.0">
<children>
<TextField fx:id="L1R6" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L2R6" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L3R6" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L4R6" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
<TextField fx:id="L5R6" editable="false" prefHeight="62.0" prefWidth="55.0" style="-fx-background-color: clear; -fx-border-color: gray; -fx-border-width: 2; -fx-text-fill: white;">
<font>
<Font name="System Bold" size="22.0" />
</font>
</TextField>
</children>
</HBox>
<Button layoutX="14.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" style="-fx-background-color: lightgray;" text="Q" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="62.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="45.0" style="-fx-background-color: lightgray;" text="W" textFill="WHITE">
<font>
<Font name="System Bold" size="19.0" />
</font>
</Button>
<Button layoutX="114.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="E" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="164.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="R" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="214.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="T" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="264.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="Y" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="314.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="U" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="364.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="I" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="414.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="O" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="464.0" layoutY="464.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="P" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="239.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="G" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="289.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="H" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="339.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="J" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="389.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="K" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="439.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="L" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="189.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="F" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="139.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="D" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="89.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="S" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="39.0" layoutY="515.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="A" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="239.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="V" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="289.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="B" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="339.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="N" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="389.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="46.0" style="-fx-background-color: lightgray;" text="M" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="189.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="C" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="139.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="X" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="89.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="42.0" style="-fx-background-color: lightgray;" text="Z" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="439.0" layoutY="566.0" mnemonicParsing="false" onAction="#inputLetter" prefHeight="43.0" prefWidth="68.0" style="-fx-background-color: lightgray;" text="<--" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button layoutX="7.0" layoutY="566.0" mnemonicParsing="false" onAction="#enterWord" prefHeight="43.0" prefWidth="76.0" style="-fx-background-color: lightgray;" text="Enter" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
</children>
</Pane>
Controller class
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.scene.input.KeyEvent;
/**
* @author medicherlas
* @version 1.0
* @created 13-Feb-2024 4:55:11 PM
*/
public class UserUI {
@FXML
private TextField L1R1 = new TextField();
@FXML
private TextField L2R1 = new TextField();
@FXML
private TextField L3R1 = new TextField();
@FXML
private TextField L4R1 = new TextField();
@FXML
private TextField L5R1 = new TextField();
@FXML
private TextField L1R2 = new TextField();
@FXML
private TextField L2R2 = new TextField();
@FXML
private TextField L3R2 = new TextField();
@FXML
private TextField L4R2 = new TextField();
@FXML
private TextField L5R2 = new TextField();
@FXML
private TextField L1R3 = new TextField();
@FXML
private TextField L2R3 = new TextField();
@FXML
private TextField L3R3 = new TextField();
@FXML
private TextField L4R3 = new TextField();
@FXML
private TextField L5R3 = new TextField();
@FXML
private TextField L1R4 = new TextField();
@FXML
private TextField L2R4 = new TextField();
@FXML
private TextField L3R4 = new TextField();
@FXML
private TextField L4R4 = new TextField();
@FXML
private TextField L5R4 = new TextField();
@FXML
private TextField L1R5 = new TextField();
@FXML
private TextField L2R5 = new TextField();
@FXML
private TextField L3R5 = new TextField();
@FXML
private TextField L4R5 = new TextField();
@FXML
private TextField L5R5 = new TextField();
@FXML
private TextField L1R6 = new TextField();
@FXML
private TextField L2R6 = new TextField();
@FXML
private TextField L3R6 = new TextField();
@FXML
private TextField L4R6 = new TextField();
@FXML
private TextField L5R6 = new TextField();
private TextField[][] letterBoxes;
private int count = 0;
public UserUI(){
}
public void finalize() throws Throwable {
}
@FXML
private void initialize(){
letterBoxes = new TextField[6][5];
letterBoxes[0][0] = L1R1;
letterBoxes[0][1] = L2R1;
letterBoxes[0][2] = L3R1;
letterBoxes[0][3] = L4R1;
letterBoxes[0][4] = L5R1;
}
/**
*
*
*/
public void enterWord(){
int guessNum = 0;
// int guessNum = getGuessCount() - 1;
String s = "";
for(int i = 0; i < letterBoxes[0].length ; i++){
s += letterBoxes[guessNum][i].getText();
}
System.out.println(s);
// Controller.takeInGuess(s);
}
public void letterFrequency(){
}
public void updateLetterBoxes(){
}
public void wordFrequency(){
}
public void inputLetter(ActionEvent actionEvent) {
String letter = ((Button) actionEvent.getSource()).getText();
int guessNum = 0;
// int guessNum = getGuessCount() - 1;
if(letter.equals("<--")){
if(count > 0) {
count--;
}
letterBoxes[guessNum][count].setText("");
} else if(count < 5){
letterBoxes[guessNum][count++].setText(letter);
}
}
@FXML
void letterTyped(KeyEvent keyEvent) {
System.out.println(keyEvent.getCode());
}
}