У меня проблема с отображением окон в моей программе. В первом окне необходимо ввести имя пользователя и пароль и выбрать один вариант (Admin / Student). Затем нажмите кнопку «Войти». Кроме того, я добавил еще одну метку с сообщением (если данные журнала хороши или нет). Но если я нажму эту кнопку, ничего не произойдет. Я не знаю почему.
LoginApp.java
public class LoginController implements Initializable {
LoginModel loginModel = new LoginModel();
@FXML
private AnchorPane ap;
@FXML
private Label loginstatus;
@FXML
private Label dbstatus;
@FXML
private TextField username;
@FXML
private PasswordField password;
@FXML
private ComboBox<option> combobox;
@FXML
private Button loginbutton;
public void initialize(URL url, ResourceBundle rb) {
if (this.loginModel.isDatabaseConnected()) {
this.dbstatus.setText("Connected to Database");
} else {
this.dbstatus.setText("Not connected to Database");
}
this.combobox.setItems(FXCollections.observableArrayList(option.values()));
}
@FXML
public void login(ActionEvent event) {
//combobox.setEditable(false);
try {
if (this.loginModel.isLogin(this.username.getText(), this.password.getText(),
((option) this.combobox.getValue()).toString())) {
Stage stage = (Stage) this.loginbutton.getScene().getWindow();
stage.close();
switch (((option) this.combobox.getValue()).toString()) {
case "Student":
studentLogin();
break;
case "Admin":
adminLogin();
System.out.println("Dadasd");
break;
}
} if (this.loginModel.isLogin(this.username.getText(), this.password.getText(),
((option) this.combobox.getValue()).toString())) {
this.loginstatus.setText("Connected");
} else
{
this.loginstatus.setText("Denied");
}
} catch (Exception localException) {
}
try {
this.username.setText("");
this.password.setText("");
}catch (Exception localException){
}
}
public void studentLogin() {
//combobox.setEditable(false);
try {
Stage userStage = new Stage();
FXMLLoader loader = new FXMLLoader();
Parent root = (Parent) loader.load(getClass().getResource("/students/studentFXML.fxml").openStream());
StudentController studentController = (StudentController) loader.getController();
Scene scene = new Scene(root);
userStage.setScene(scene);
userStage.setTitle("Student Board");
userStage.setResizable(false);
userStage.show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void adminLogin() {
//combobox.setEditable(false);
try {
Stage adminStage = new Stage();
FXMLLoader adminLoader = new FXMLLoader();
Pane adminroot = (Pane) adminLoader.load(getClass().getResource("/admin/tabs/admin.fxml").openStream());
AdminController adminController = (AdminController) adminLoader.getController();
Scene scene = new Scene(adminroot);
adminStage.setScene(scene);
adminStage.setTitle("Admin Board");
adminStage.setResizable(false);
adminStage.show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
login.fxml
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginapp.LoginController">
<children>
<Label layoutX="14.0" layoutY="14.0" prefHeight="17.0" prefWidth="86.0" text="DB status" />
<Label fx:id="dbstatus" layoutX="101.0" layoutY="14.0" prefHeight="17.0" prefWidth="216.0" />
<Label fx:id="loginstatus" layoutX="14.0" layoutY="217.0" prefHeight="17.0" prefWidth="79.0" />
<TextField fx:id="username" layoutX="101.0" layoutY="79.0" prefHeight="25.0" prefWidth="216.0" promptText="user name" />
<Label layoutX="14.0" layoutY="83.0" prefHeight="17.0" prefWidth="79.0" text="UserName" />
<Label layoutX="14.0" layoutY="124.0" prefHeight="17.0" prefWidth="79.0" text="Password" />
<PasswordField fx:id="password" layoutX="101.0" layoutY="120.0" prefHeight="25.0" prefWidth="216.0" promptText="password" />
<ComboBox fx:id="combobox" layoutX="101.0" layoutY="169.0" prefHeight="25.0" prefWidth="216.0" promptText="Admin/Student" />
<Button fx:id="loginbutton" layoutX="100.0" layoutY="213.0" mnemonicParsing="false" onAction="#login" prefHeight="25.0" prefWidth="216.0" text="Login" />
</children>
</AnchorPane>
StudentController.java
public class StudentController implements Initializable {
@FXML
private TextField firstname;
@FXML
private TextField lastname;
@FXML
private TextField email;
@FXML
private DatePicker dob;
@FXML
private TableView<StudentData> studenttable;
@FXML
private TableColumn<StudentData, String> firstnamecolumn;
@FXML
private TableColumn<StudentData, String> lastnamecolumn;
@FXML
private TableColumn<StudentData, String> emailcolumn;
@FXML
private TableColumn<StudentData, String> dobcolumn;
private ObservableList<StudentData> data;
private dbConnection dc;
private String sql = "SELECT * FROM students";
public void initialize(URL url, ResourceBundle rb) {
this.dc = new dbConnection();
}
@FXML
private void loadStudentData(ActionEvent event) throws SQLException {
try {
Connection conn = dbConnection.getConnection();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()) {
this.data.add(new StudentData(rs.getString(1), rs.getString(2), rs.getString(3),
rs.getString(4))); }
} catch (SQLException ex) {
System.err.println("Error : " + ex);
}
this.firstnamecolumn.setCellValueFactory(new PropertyValueFactory<StudentData, String>("firstName"));
this.lastnamecolumn.setCellValueFactory(new PropertyValueFactory<StudentData, String>("lastName"));
this.emailcolumn.setCellValueFactory(new PropertyValueFactory<StudentData, String>("email"));
this.dobcolumn.setCellValueFactory(new PropertyValueFactory<StudentData, String>("DOB"));
this.studenttable.setItems(null);
this.studenttable.setItems(this.data);
}
@FXML
private void addStudent (ActionEvent event) {
String sqlAdd = "INSERT INTO students (firstName, lastName, email, DOB) VALUES (?, ?, ?, ?)";
try {
Connection conn = dbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sqlAdd);
stmt.setString(2, this.firstname.getText());
stmt.setString(3, this.lastname.getText());
stmt.setString(4, this.email.getText());
stmt.setString(5, this.dob.getEditor().getText());
stmt.execute();
stmt.close();
}catch (SQLException ex) {
System.err.println("Error :" + ex);
}
}
@FXML
private void clearFields(ActionEvent event) {
this.firstname.setText("");
this.lastname.setText("");
this.email.setText("");
this.dob.setValue(null);
}
}
studentFXML.fxml
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="360.0"
prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="students.StudentController">
<children>
<TabPane prefHeight="360.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Student">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="360.0" prefWidth="640.0">
<children>
<HBox layoutX="156.0" prefHeight="330.0" prefWidth="490.0">
<children>
<TableView fx:id="studenttable" prefHeight="330.0" prefWidth="490.0">
<columns>
<TableColumn fx:id="idcolumn" prefWidth="75.0" text="ID"/>
<TableColumn fx:id="firstnamecolumn" prefWidth="106.0"
text="First Name"/>
<TableColumn fx:id="lastnamecolumn" prefWidth="112.0" text="Last Name"/>
<TableColumn fx:id="emailcolumn" prefWidth="108.0" text="Email"/>
<TableColumn fx:id="dobcolumn" minWidth="0.0" prefWidth="82.0"
text="DOB"/>
</columns>
</TableView>
</children>
</HBox>
<VBox prefHeight="337.0" prefWidth="159.0">
<children>
<Label prefHeight="19.0" prefWidth="160.0" text="Add student">
<padding>
<Insets left="10.0"/>
</padding>
<VBox.margin>
<Insets/>
</VBox.margin>
</Label>
<TextField fx:id="id">
<VBox.margin>
<Insets left="10.0" right="10.0" top="10.0"/>
</VBox.margin>
</TextField>
<TextField fx:id="firstname" layoutX="10.0" layoutY="29.0"
promptText="First Name">
<VBox.margin>
<Insets left="10.0" right="10.0"/>
</VBox.margin>
</TextField>
<TextField fx:id="lastname" layoutX="10.0" layoutY="54.0"
promptText="Last Name">
<VBox.margin>
<Insets left="10.0" right="10.0"/>
</VBox.margin>
</TextField>
<TextField fx:id="email" layoutX="10.0" layoutY="54.0" promptText="Email">
<VBox.margin>
<Insets left="10.0" right="10.0"/>
</VBox.margin>
</TextField>
<DatePicker fx:id="dob" prefWidth="200.0">
<VBox.margin>
<Insets left="10.0" right="10.0"/>
</VBox.margin>
</DatePicker>
<Button mnemonicParsing="false" onAction="#addStudent" prefHeight="17.0"
prefWidth="140.0" text="Add Data">
<VBox.margin>
<Insets left="10.0"/>
</VBox.margin>
</Button>
<Button layoutX="20.0" layoutY="164.0" mnemonicParsing="false"
onAction="#clearFields" prefHeight="17.0" prefWidth="140.0"
text="Clear Form">
<VBox.margin>
<Insets left="10.0"/>
</VBox.margin>
</Button>
<Button layoutX="20.0" layoutY="189.0" mnemonicParsing="false"
onAction="#loadStudentData" prefHeight="17.0" prefWidth="140.0"
text="Load Form">
<VBox.margin>
<Insets left="10.0"/>
</VBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="152.0"
text="Delete Data">
<VBox.margin>
<Insets left="10.0" right="10.0"/>
</VBox.margin>
</Button>
</children>
</VBox>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
option.java
public enum option {
Admin, Student;
private option() {}
public String value() {
return name();
}
public static option fromvalue (String v) {
return valueOf(v);
}
}