Я пытаюсь добавить слушателя в текстовое поле в контроллере, но cudnt сделать это. я не могу найти какие-либо опции текстового поля в контроллере, используя fx: id, заданный для этого конкретного текстового поля.
sample.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.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.effect.Reflection?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="294.0" prefWidth="883.0" style="-fx-background-color: #000000;" stylesheets="@style.css" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler">
<children>
<Label layoutX="28.0" layoutY="55.0" text="Enter Text : " AnchorPane.leftAnchor="28.0">
<font>
<Font size="15.0" />
</font>
</Label>
<TextField fx:id="msg_tb" alignment="CENTER" layoutX="117.0" layoutY="52.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="@style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<TextField fx:id="n_msg_tb" alignment="CENTER" layoutX="117.0" layoutY="93.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="@style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<Button fx:id="reset_b" alignment="CENTER" layoutX="602.0" layoutY="171.0" mnemonicParsing="false" onAction="#reset" style="-fx-text-fill: #FFFFFF; -fx-background-radius: 20;" stylesheets="@style.css" text="RESET" AnchorPane.rightAnchor="161.0">
<font>
<Font name="Calibri Bold" size="31.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
<VBox layoutX="495.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="@style.css" AnchorPane.rightAnchor="221.0">
<children>
<RadioButton fx:id="er" mnemonicParsing="false" onAction="#er_action" prefHeight="17.0" prefWidth="92.0" stylesheets="@application.css" text="ENCRIPTION" textFill="#797979">
<toggleGroup>
<ToggleGroup fx:id="group1" />
</toggleGroup>
</RadioButton>
<AnchorPane fx:id="ebox" prefHeight="150.0" prefWidth="167.0">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="@application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_nl" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="@application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_at" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="@style.css" text="CEASER">
<graphic>
<TextField fx:id="e_key_tb" onAction="#e_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="@style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button alignment="CENTER" disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="@application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
<VBox layoutX="662.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="@style.css" AnchorPane.rightAnchor="54.0">
<children>
<RadioButton fx:id="dr" mnemonicParsing="false" onAction="#dr_action" prefHeight="17.0" prefWidth="98.0" stylesheets="@application.css" text="DECRIPTION" textFill="#797979" toggleGroup="$group1" />
<AnchorPane fx:id="dbox" prefHeight="150.0" prefWidth="167.0" visible="false">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="@application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="@application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="@style.css" text="CEASER">
<graphic>
<TextField fx:id="d_key_tb" onAction="#d_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="@style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#d_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="@application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</fx:root>
controler.java
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class controler implements Initializable {
@FXML
private TextField msg_tb;
@FXML
private TextField n_msg_tb;
@FXML
private Button reset_b;
@FXML
private RadioButton er;
@FXML
private ToggleGroup group1;
@FXML
private RadioButton dr;
@FXML
private AnchorPane ebox;
@FXML
private AnchorPane dbox;
@FXML
public TextField e_key_tb;
@FXML
private TextField d_key_tb;
@FXML
void d_c(ActionEvent event) {
}
@FXML
void d_c_key_tb(ActionEvent event) {
}
@FXML
void dr_action(ActionEvent event) {
dbox.setVisible(true);
dbox.setDisable(false);
ebox.setVisible(false);
ebox.setDisable(true);
}
@FXML
void e_at(ActionEvent event) {
}
@FXML
void e_c(ActionEvent event) {
}
@FXML
void e_c_key_tb(ActionEvent event) {
}
@FXML
void e_nl(ActionEvent event) {
}
@FXML
void er_action(ActionEvent event) {
ebox.setVisible(true);
ebox.setDisable(false);
dbox.setVisible(false);
dbox.setDisable(true);
}
@FXML
void reset(ActionEvent event) {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
ArrayList<morse> mkey = new ArrayList<>();
try {
morse.load(mkey);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public TextField getMsg_tb() {
return msg_tb;
}
public void setMsg_tb(TextField msg_tb) {
this.msg_tb = msg_tb;
}
public TextField getN_msg_tb() {
return n_msg_tb;
}
public void setN_msg_tb(TextField n_msg_tb) {
this.n_msg_tb = n_msg_tb;
}
public Button getReset_b() {
return reset_b;
}
public void setReset_b(Button reset_b) {
this.reset_b = reset_b;
}
public RadioButton getEr() {
return er;
}
public void setEr(RadioButton er) {
this.er = er;
}
public ToggleGroup getGroup1() {
return group1;
}
public void setGroup1(ToggleGroup group1) {
this.group1 = group1;
}
public RadioButton getDr() {
return dr;
}
public void setDr(RadioButton dr) {
this.dr = dr;
}
public AnchorPane getEbox() {
return ebox;
}
public void setEbox(AnchorPane ebox) {
this.ebox = ebox;
}
public AnchorPane getDbox() {
return dbox;
}
public void setDbox(AnchorPane dbox) {
this.dbox = dbox;
}
public TextField getE_key_tb() {
return e_key_tb;
}
public void setE_key_tb(TextField e_key_tb) {
this.e_key_tb = e_key_tb;
}
public TextField getD_key_tb() {
return d_key_tb;
}
public void setD_key_tb(TextField d_key_tb) {
this.d_key_tb = d_key_tb;
}
e_key_tb.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+ oldText +" to "+newText);
});
}
}
когда я пытаюсь добавить слушателя в конце, он не работает. Я не могу получить никаких опций для текстового поля, когда я делаю e_key_tb и Ctrl + пробел, где он должен дать выпадающий список с кучей предложений ... Я использовал конструктор сцены, чтобы предоставить идентификатор и скопировал контроллер скелитона из конструктора сцены ... позже, когда я пытаюсь чтобы использовать текстовое поле, оно заставило меня создавать методы получения и установки для всех текстовых полей. Можете ли вы объяснить, что это происходит, и я никогда раньше не использовал слушателей.