JavaFx ComboBox 在第一次点击时没有获得正确的值
JavaFx ComboBox is not getting correct value on first click
我有一个来自 java FXML 的组合框,我用我的数组列表填充了它。当我第一次单击组合框时,它 return 为空,但当我再次单击它时,它将 return 为正确的值。真正应该发生的是,当我第一次点击组合框时,它应该 return 第一次点击而不是第二次点击的正确值。
我有什么:
package main.controller.manageAccounts;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import main.model.MangeAccounts.ManageAccountsModel;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class ManageAccountsController implements Initializable {
public ManageAccountsModel manageAccountsModel = new ManageAccountsModel();
@FXML
private ComboBox<String> customerDropDownList;
@FXML
private TextField txtUsername, txtFirstName, txtLastName, txtPassword, txtSecretQuestion, txtSecretQuestionAnswer;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
populateCustomerdropDownList();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (manageAccountsModel.isDbConnected()) {
System.out.println("Database connection established");
} else {
System.out.println("Database connection not established");
}
}
public void populateCustomerdropDownList() throws SQLException {
customerDropDownList.setItems(manageAccountsModel.getEmployeeId());
}
public void setFields(MouseEvent mouseEvent) throws SQLException {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
}
}
我尝试过的:
public void populateCustomerdropDownList() throws SQLException {
customerDropDownList.setItems(manageAccountsModel.getEmployeeId());
}
public void setFields(MouseEvent mouseEvent) throws SQLException {
customerDropDownList.addEventFilter(MouseEvent.MOUSE_CLICKED, (event) -> {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
try {
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
} catch (SQLException e) {
System.out.println("Work?");
}
});
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.controller.manageAccounts.ManageAccountsController">
<children>
<StackPane prefHeight="400.0" prefWidth="600.0">
<children>
<ComboBox fx:id="customerDropDownList" onMouseClicked="#setFields" prefWidth="150.0" translateY="80.0" StackPane.alignment="TOP_CENTER" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Customer" translateY="60.0" StackPane.alignment="TOP_CENTER" />
<TextField fx:id="txtUsername" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Username" translateY="-60.0" />
<TextField fx:id="txtFirstName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change First Name" translateY="-25.0" />
<TextField fx:id="txtLastName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Last Name" translateY="10.0" />
<TextField fx:id="txtPassword" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Password" translateY="45.0" />
<TextField fx:id="txtSecretQuestion" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Secret Question" translateY="80.0" />
<TextField fx:id="txtSecretQuestionAnswer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Secret Answer Question" translateY="115.0" />
<Button mnemonicParsing="false" text="Button" translateY="-30.0" StackPane.alignment="BOTTOM_CENTER" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Manage User Accounts" translateY="20.0" StackPane.alignment="TOP_CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Text>
</children>
</StackPane>
</children>
</AnchorPane>
更正修复,以便第一次单击将获得选定的值,而不是返回 null 并使用户必须再次单击组合框是:
如 James 所述,将 FXML 文件 onMouseClicked="#setFields"
更改为 onAction="#setFields"
,并将 public void setFields(MouseEvent mouseEvent)
更改为 public void setFields(ActionEvent event)
执行此操作后,一旦用户单击下拉菜单,值将立即被采用。
更新代码
public void setFields(ActionEvent event) throws SQLException {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
try {
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
} catch (SQLException e) {
System.out.println("Work?");
}
}
我有一个来自 java FXML 的组合框,我用我的数组列表填充了它。当我第一次单击组合框时,它 return 为空,但当我再次单击它时,它将 return 为正确的值。真正应该发生的是,当我第一次点击组合框时,它应该 return 第一次点击而不是第二次点击的正确值。
我有什么:
package main.controller.manageAccounts;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import main.model.MangeAccounts.ManageAccountsModel;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class ManageAccountsController implements Initializable {
public ManageAccountsModel manageAccountsModel = new ManageAccountsModel();
@FXML
private ComboBox<String> customerDropDownList;
@FXML
private TextField txtUsername, txtFirstName, txtLastName, txtPassword, txtSecretQuestion, txtSecretQuestionAnswer;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
populateCustomerdropDownList();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (manageAccountsModel.isDbConnected()) {
System.out.println("Database connection established");
} else {
System.out.println("Database connection not established");
}
}
public void populateCustomerdropDownList() throws SQLException {
customerDropDownList.setItems(manageAccountsModel.getEmployeeId());
}
public void setFields(MouseEvent mouseEvent) throws SQLException {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
}
}
我尝试过的:
public void populateCustomerdropDownList() throws SQLException {
customerDropDownList.setItems(manageAccountsModel.getEmployeeId());
}
public void setFields(MouseEvent mouseEvent) throws SQLException {
customerDropDownList.addEventFilter(MouseEvent.MOUSE_CLICKED, (event) -> {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
try {
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
} catch (SQLException e) {
System.out.println("Work?");
}
});
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.controller.manageAccounts.ManageAccountsController">
<children>
<StackPane prefHeight="400.0" prefWidth="600.0">
<children>
<ComboBox fx:id="customerDropDownList" onMouseClicked="#setFields" prefWidth="150.0" translateY="80.0" StackPane.alignment="TOP_CENTER" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Customer" translateY="60.0" StackPane.alignment="TOP_CENTER" />
<TextField fx:id="txtUsername" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Username" translateY="-60.0" />
<TextField fx:id="txtFirstName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change First Name" translateY="-25.0" />
<TextField fx:id="txtLastName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Last Name" translateY="10.0" />
<TextField fx:id="txtPassword" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Password" translateY="45.0" />
<TextField fx:id="txtSecretQuestion" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Secret Question" translateY="80.0" />
<TextField fx:id="txtSecretQuestionAnswer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="200.0" text="Change Secret Answer Question" translateY="115.0" />
<Button mnemonicParsing="false" text="Button" translateY="-30.0" StackPane.alignment="BOTTOM_CENTER" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Manage User Accounts" translateY="20.0" StackPane.alignment="TOP_CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Text>
</children>
</StackPane>
</children>
</AnchorPane>
更正修复,以便第一次单击将获得选定的值,而不是返回 null 并使用户必须再次单击组合框是:
如 James 所述,将 FXML 文件 onMouseClicked="#setFields"
更改为 onAction="#setFields"
,并将 public void setFields(MouseEvent mouseEvent)
更改为 public void setFields(ActionEvent event)
执行此操作后,一旦用户单击下拉菜单,值将立即被采用。
更新代码
public void setFields(ActionEvent event) throws SQLException {
String getEmployeeId = customerDropDownList.getValue();
int employeeId = Integer.parseInt(getEmployeeId);
try {
txtUsername.setText(manageAccountsModel.getUserName(employeeId));
txtFirstName.setText(manageAccountsModel.getFirstName(employeeId));
txtLastName.setText(manageAccountsModel.getLastName(employeeId));
txtPassword.setText(manageAccountsModel.getPassword(employeeId));
txtSecretQuestion.setText(manageAccountsModel.getSecretQuestion(employeeId));
txtSecretQuestionAnswer.setText(manageAccountsModel.getSecretQuestionAnswer(employeeId));
System.out.println(employeeId);
} catch (SQLException e) {
System.out.println("Work?");
}
}