- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试通过 setter 方法在 java 中的多个场景之间传递值。但是当我尝试这样做时,NullPointerException
弹出。我也想将电子邮件(用户名)保留在其他 Controller 中以识别用户。这是我的登录代码。
public class Controller {
public JFXTextField newsletterEmail;
public JFXButton regButton;
public JFXTextField loginUserName;
public JFXPasswordField loginPassword;
public JFXButton loginButton;
Connectivity connection = new Connectivity();
Connection connec = connection.getConnection();
SceneSwitcher sceneSwitcher = new SceneSwitcher();
ViewMyAccount viewMyAccount = new ViewMyAccount();
loginValidation validateLogin = new loginValidation();
public void loginButtonClicked(ActionEvent actionEvent) {
System.out.println(loginUserName.getText());
boolean validateCustomer = validateLogin.CusLoginValidate(loginUserName.getText(),loginPassword.getText(),connec);
boolean validateStaff = validateLogin.StaffLoginValidate(loginUserName.getText(),loginPassword.getText(),connec);
boolean validateOwner = validateLogin.OwnerLoginValidate(loginUserName.getText(),loginPassword.getText(),connec);
if(loginUserName.getText().equals("") || loginPassword.getText().equals("")){
AlertBox.displayAlertBox("ERROR!","Both fields can't be empty!");
}else{
if(validateCustomer){
sceneSwitcher.switchScene(loginButton,"customerView.fxml","Customer");
}else if(validateStaff){
sceneSwitcher.switchScene(loginButton,"staffView.fxml","Customer");
}else if(validateOwner){
sceneSwitcher.switchScene(loginButton,"ownerView.fxml","Customer");
}else{
AlertBox.displayAlertBox("ERROR!","Invalid Username or Password! ");
}
}
}
public void registerButtonClicked(ActionEvent actionEvent) {
sceneSwitcher.switchScene(regButton,"register.fxml","Register");
}
NewsletterValidation validateEmail = new NewsletterValidation();
public void newsletterButtonClicked(ActionEvent actionEvent) throws SQLException {
boolean isNewsletterEmailEmpty = validateEmail.invalidError(newsletterEmail);
boolean isValid = validateEmail.isValidEmailAddress(newsletterEmail);
boolean isEmailExist = validateEmail.checkEmailExists(newsletterEmail.getText(),connec);
if(isNewsletterEmailEmpty && isValid && isEmailExist){
PreparedStatement pstmt = null;
String sql = "INSERT INTO `nwemails` (`email`)\n" +
"VALUES (?);";
try {
pstmt = connec.prepareStatement(sql);
pstmt.setString(1,newsletterEmail.getText());
int i = pstmt.executeUpdate();
System.out.println("newsletter email update status = " + i);
AlertBox.displayAlertBox("Alert!","You have successfully signed up for the news letter");
} catch (SQLException e) {
System.err.println(e);
}finally {
pstmt.close();
}
}else{
System.out.println("Validation failed");
}
}
}
这是我的登录界面:
我想将值传递给这个 Controller :
public class ViewMyAccount implements Initializable {
public Label errorPassword;
public Label errorMobile;
public Label errorEmail;
public Label errorLastName;
public Label errorFirstName;
public TextField notes;
public TextField address;
public PasswordField confirmPassword;
public PasswordField password;
public TextField occupation;
public TextField mobile;
public TextField email;
public TextField lastName;
public TextField firstName;
public Button updateButton;
public Button backButton;
Connectivity connection = new Connectivity();
Connection connec = connection.getConnection();
registerValidation validate = new registerValidation();
SceneSwitcher sceneSwitcher = new SceneSwitcher();
public String mail = "mangalika@gmail.com"; //I want the logged in mail to assign this
public void initialize(URL url, ResourceBundle rb) {
//fill info in fields
PreparedStatement pstmtGetInfo = null;
ResultSet rslt = null;
String sqlGetInfo = "SELECT * FROM users WHERE email=?";
System.out.println(mail);
try {
pstmtGetInfo = connec.prepareStatement(sqlGetInfo);
pstmtGetInfo.setString(1,mail);
rslt = pstmtGetInfo.executeQuery();
if(rslt.next()){
StringBuffer stringBuffer1 = new StringBuffer();
stringBuffer1.append(rslt.getString("firstName"));
firstName.setText(stringBuffer1.toString());
StringBuffer stringBuffer2 = new StringBuffer();
stringBuffer2.append(rslt.getString("lastName"));
lastName.setText(stringBuffer2.toString());
StringBuffer stringBuffer3 = new StringBuffer();
stringBuffer3.append(rslt.getString("email"));
email.setText(stringBuffer3.toString());
StringBuffer stringBuffer4 = new StringBuffer();
stringBuffer4.append(rslt.getString("mobile"));
mobile.setText(stringBuffer4.toString());
StringBuffer stringBuffer5 = new StringBuffer();
stringBuffer5.append(rslt.getString("occupation"));
occupation.setText(stringBuffer5.toString());
StringBuffer stringBuffer6 = new StringBuffer();
stringBuffer6.append(rslt.getString("address"));
address.setText(stringBuffer6.toString());
StringBuffer stringBuffer7 = new StringBuffer();
stringBuffer7.append(rslt.getString("password"));
password.setText(stringBuffer7.toString());
confirmPassword.setText(stringBuffer7.toString());
StringBuffer stringBuffer8 = new StringBuffer();
stringBuffer8.append(rslt.getString("notes"));
notes.setText(stringBuffer8.toString());
}else{
System.out.println("no rows for this mail ID");
}
} catch (SQLException e) {
System.err.println(e);
}
}
public void updateButtonClicked(ActionEvent actionEvent) throws SQLException{
boolean isFirstNameEmpty = validate.emptyError(firstName,errorFirstName,"First name is required!");
boolean isFirstNameNum = validate.isNotNum(firstName,errorFirstName,"First name can't be numbers!");
boolean isLastNameEmpty = validate.emptyError(lastName,errorLastName,"Last name is required!");
boolean isLastNameNum = validate.isNotNum(lastName,errorLastName,"Last name can't be numbers!");
boolean isEmailEmpty = validate.emptyError(email,errorEmail,"Email address is required!");
boolean isPasswordEmpty = validate.emptyError(password,errorPassword,"Password is required!");
boolean isemailValid = validate.isValidEmailAddress(email);
boolean isPasswordValid = validate.isValidPassword(password,confirmPassword,errorPassword);
boolean isPasswordMatched = validate.isPasswordMatch(password,confirmPassword);
boolean isUserNameExist = validate.checkUsernameExists(email.getText(),connec,errorEmail);
if(isFirstNameEmpty && isFirstNameNum && isLastNameEmpty && isLastNameNum && isEmailEmpty && isPasswordEmpty && isPasswordValid && isemailValid && isPasswordMatched && isUserNameExist){
PreparedStatement pstmt = null;
String sql = "UPDATE users\n" +
"SET firstName = ?, lastName= ?, email= ?, mobile= ?, occupation= ?, address= ?, password= ?, notes= ?\n" +
"WHERE email = ?;";
try {
pstmt = connec.prepareStatement(sql);
pstmt.setString(1,firstName.getText());
pstmt.setString(2,lastName.getText());
pstmt.setString(3,email.getText());
pstmt.setString(4,mobile.getText());
pstmt.setString(5,occupation.getText());
pstmt.setString(6,address.getText());
pstmt.setString(7,password.getText());
pstmt.setString(8,notes.getText());
pstmt.setString(9,mail);
int i = pstmt.executeUpdate();
System.out.println("customer info UPDATE status = " + i);
if(i == 1){
AlertBox.displayAlertBox("Congratulations!","You have successfully registered to JFS");
sceneSwitcher.switchScene(updateButton,"sample.fxml","JFS");
}else{
AlertBox.displayAlertBox("Error","Database couldn't be updated");
}
} catch (SQLException e) {
System.err.println(e);
}finally {
pstmt.close();
}
AlertBox.displayAlertBox("Congratulations!","You have successfully updated your account");
sceneSwitcher.switchScene(updateButton,"sample.fxml","JFS");
}else{
System.out.println("Validation failed");
}
}
public void backButtonClicked(ActionEvent actionEvent) {
sceneSwitcher.switchScene(backButton,"customerView.fxml","Welcome to JFS ");
}
}
我想获取用户在登录屏幕中输入的电子邮件,以便在此 Controller 中使用。因为我需要该电子邮件来通过流程识别用户。但它显示为空。请帮我找出问题所在。
最佳答案
出现问题是因为在尝试将值传递到的 ViewMyActivity Controller 中调用初始化之前,您没有在代码中设置 mail 变量。
使用下面的代码创建一个项目并进行测试。这展示了如何在一个简单但主要是功能性的 JavaFX FXML 布局中的 Controller 之间传递值。
第一个loginLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="FXMLController"
xmlns:fx="http://javafx.com/fxml"
alignment="center"
hgap="10"
vgap="10"
styleClass="root">
<Text id="welcome-text" text="Login Below:"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2"/>
<padding><Insets top="25" right="25" bottom="10" left="25" /></padding>
<Label text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="usernameField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<HBox spacing="10" alignment="bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="Login"
onAction="#handleLogin" />
</HBox>
<Text fx:id="usernameEntered" GridPane.columnIndex="1" GridPane.rowIndex="6" />
</GridPane>
现在FXMLController:
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
public class FXMLController {
ViewMyAccount viewMyAccount = new ViewMyAccount();
@FXML private Text usernameEntered;
@FXML private TextField usernameField;
@FXML protected void handleLogin(ActionEvent event) {
String enteredValue = usernameField.getText().toString();
usernameEntered.setText("Username: " + enteredValue);
System.out.println("From Controller: " + enteredValue);
System.out.println("From ViewMyAccount: "+
viewMyAccount.setAndReturnMail(enteredValue));
//I cannot tell why you want to call initialize from
//ViewMyAccount, but this is a test showing it works
viewMyAccount.initialize(null, null);
}
}
现在 FXMLMain,运行这个类:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLMain extends Application {
public static void main(String[] args) {
//you should know this but this is how you launch your application
Application.launch(FXMLMain.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("loginLayout.fxml"));
stage.setTitle("Login");
stage.setScene(new Scene(root, 600, 475));
stage.show();
}
}
现在是你的 ViewMyAccount 类,请注意,我确实注释掉了没有代码我无法运行的所有“片段”:
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.event.ActionEvent;
public class ViewMyAccount implements Initializable {
public Label errorPassword;
public Label errorMobile;
public Label errorEmail;
public Label errorLastName;
public Label errorFirstName;
public TextField notes;
public TextField address;
public PasswordField confirmPassword;
public PasswordField password;
public TextField occupation;
public TextField mobile;
public TextField email;
public TextField lastName;
public TextField firstName;
public Button updateButton;
public Button backButton;
//I don't have this code...
//Connectivity connection = new Connectivity();
//Connection connec = connection.getConnection();
//registerValidation validate = new registerValidation();
//SceneSwitcher sceneSwitcher = new SceneSwitcher();
public String mail = "mangalika@gmail.com"; //I want the logged in mail to assign this
public String setAndReturnMail(String userEnteredValue) {
this.mail = userEnteredValue;
return this.mail;
}
public void initialize(URL url, ResourceBundle rb) {
//fill info in fields
PreparedStatement pstmtGetInfo = null;
ResultSet rslt = null;
String sqlGetInfo = "SELECT * FROM users WHERE email=?";
System.out.println(mail);
/*
try {
pstmtGetInfo = connec.prepareStatement(sqlGetInfo);
pstmtGetInfo.setString(1,mail);
rslt = pstmtGetInfo.executeQuery();
if(rslt.next()){
StringBuffer stringBuffer1 = new StringBuffer();
stringBuffer1.append(rslt.getString("firstName"));
firstName.setText(stringBuffer1.toString());
StringBuffer stringBuffer2 = new StringBuffer();
stringBuffer2.append(rslt.getString("lastName"));
lastName.setText(stringBuffer2.toString());
StringBuffer stringBuffer3 = new StringBuffer();
stringBuffer3.append(rslt.getString("email"));
email.setText(stringBuffer3.toString());
StringBuffer stringBuffer4 = new StringBuffer();
stringBuffer4.append(rslt.getString("mobile"));
mobile.setText(stringBuffer4.toString());
StringBuffer stringBuffer5 = new StringBuffer();
stringBuffer5.append(rslt.getString("occupation"));
occupation.setText(stringBuffer5.toString());
StringBuffer stringBuffer6 = new StringBuffer();
stringBuffer6.append(rslt.getString("address"));
address.setText(stringBuffer6.toString());
StringBuffer stringBuffer7 = new StringBuffer();
stringBuffer7.append(rslt.getString("password"));
password.setText(stringBuffer7.toString());
confirmPassword.setText(stringBuffer7.toString());
StringBuffer stringBuffer8 = new StringBuffer();
stringBuffer8.append(rslt.getString("notes"));
notes.setText(stringBuffer8.toString());
}else{
System.out.println("no rows for this mail ID");
}
} catch (SQLException e) {
System.err.println(e);
}
*/
}
public void updateButtonClicked(ActionEvent actionEvent) throws SQLException{
/* boolean isFirstNameEmpty = validate.emptyError(firstName,errorFirstName,"First name is required!");
boolean isFirstNameNum = validate.isNotNum(firstName,errorFirstName,"First name can't be numbers!");
boolean isLastNameEmpty = validate.emptyError(lastName,errorLastName,"Last name is required!");
boolean isLastNameNum = validate.isNotNum(lastName,errorLastName,"Last name can't be numbers!");
boolean isEmailEmpty = validate.emptyError(email,errorEmail,"Email address is required!");
boolean isPasswordEmpty = validate.emptyError(password,errorPassword,"Password is required!");
boolean isemailValid = validate.isValidEmailAddress(email);
boolean isPasswordValid = validate.isValidPassword(password,confirmPassword,errorPassword);
boolean isPasswordMatched = validate.isPasswordMatch(password,confirmPassword);
boolean isUserNameExist = validate.checkUsernameExists(email.getText(),connec,errorEmail);
if(isFirstNameEmpty && isFirstNameNum && isLastNameEmpty && isLastNameNum && isEmailEmpty && isPasswordEmpty && isPasswordValid && isemailValid && isPasswordMatched && isUserNameExist){
PreparedStatement pstmt = null;
String sql = "UPDATE users\n" +
"SET firstName = ?, lastName= ?, email= ?, mobile= ?, occupation= ?, address= ?, password= ?, notes= ?\n" +
"WHERE email = ?;";
try {
pstmt = connec.prepareStatement(sql);
pstmt.setString(1,firstName.getText());
pstmt.setString(2,lastName.getText());
pstmt.setString(3,email.getText());
pstmt.setString(4,mobile.getText());
pstmt.setString(5,occupation.getText());
pstmt.setString(6,address.getText());
pstmt.setString(7,password.getText());
pstmt.setString(8,notes.getText());
pstmt.setString(9,mail);
int i = pstmt.executeUpdate();
System.out.println("customer info UPDATE status = " + i);
if(i == 1){
AlertBox.displayAlertBox("Congratulations!","You have successfully registered to JFS");
sceneSwitcher.switchScene(updateButton,"sample.fxml","JFS");
}else{
AlertBox.displayAlertBox("Error","Database couldn't be updated");
}
} catch (SQLException e) {
System.err.println(e);
}finally {
pstmt.close();
}
AlertBox.displayAlertBox("Congratulations!","You have successfully updated your account");
sceneSwitcher.switchScene(updateButton,"sample.fxml","JFS");
}else{
System.out.println("Validation failed");
}
*/
}
public void backButtonClicked(ActionEvent actionEvent) {
// sceneSwitcher.switchScene(backButton,"customerView.fxml","Welcome to JFS ");
}
}
关于java - 当我尝试通过 javaFX 中的不同场景传递值时,Setter 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565220/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!