问题描述
我正在使用IntelliJ和scala插件开发Scala / JavaFX项目。
Java访问fxml文件元素的典型方法是为每个元素设置一个id想要访问,然后在你的controllerclass中声明一个变量,如@FXML private Label mylabelid;。
在Scala中,它的工作方式与我在源代码中看到的几乎相同。但是我在第73行得到了一个NullPointerException(标有PROBLEM-comment)。这是唯一的标签,只是设置为null。我尝试了不同的东西。除了missingInputLabel之外,每个元素都按预期设置。
LoginView.fxml:
I´m working on a Scala/JavaFX project with IntelliJ and a plugin for scala.
The typical way in Java to access elements of your fxml-file is to set an id for each element you want to access and then in your controllerclass declare a variable like "@FXML private Label mylabelid;".
In Scala it works almost the same way as you can see in my source code. BUT i get a NullPointerException in line 73 (marked with a PROBLEM-comment). This is the only label, which is just set to null. I tried diffrent things. Every single element is getting set as expected apart from missingInputLabel.
LoginView.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="logingui.LoginController">
<stylesheets>
<URL value="@/data/gtevStyle.css" />
</stylesheets>
<Label text="SSH-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<TextField fx:id="sshNameField" promptText="SSH-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="SSH-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<PasswordField fx:id="sshPasswdField" promptText="SSH-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Datenbank-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="databaseNameField" promptText="Datenbank-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Datenbank-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="3" />
<PasswordField fx:id="databasePasswdField" promptText="Datenbank-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Name oder Passwort falsch" fx:id="nameOrPasswdWrong" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label text="Fehlende Angaben" fx:id="missingInputLabel" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button text="Login" onAction="#login" GridPane.columnIndex="0" GridPane.rowIndex="5" />
</GridPane>
LoginController.scala:
LoginController.scala:
package logingui
import java.net.URL
import java.sql.SQLException
import java.util.ResourceBundle
import java.util.logging.Level
import java.util.logging.Logger
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Label
import javafx.scene.control.PasswordField
import javafx.scene.control.TextField
import javafx.scene.input.KeyEvent
import javafx.scene.input.KeyCode
import connection.GTEVConnection
import javafx.stage.Stage
class LoginController extends Initializable {
@FXML
private var nameOrPasswdWrong: Label = _
@FXML
private var missingInputLabel: Label = _ //TODO missingInputLabel is set to null?!
@FXML
private var sshNameField: TextField = _
@FXML
private var databaseNameField: TextField = _
@FXML
private var sshPasswdField: PasswordField = _
@FXML
private var databasePasswdField: PasswordField = _
//Um einfach auf alle Text-/Passwortfelder referenzieren zu können
private var textFields: Array[TextField] = Array[TextField](sshNameField, databaseNameField, sshPasswdField, databasePasswdField)
var callbackWhenFinished: () => Unit = () => GTEVConnection.close() //Default: Simply close GTEVConnection
def onKeyReleased_textField(ev: KeyEvent) { //TODO declare def as private and mark with @FXML?
if (ev.getCode == KeyCode.ENTER) {
login()
} else {
nameOrPasswdWrong.setVisible(false)
}
}
def login() {
checkInputOfTextFields()
try {
if (!missingInputLabel.isVisible) {
GTEVConnection.createConnection(sshNameField.getText, sshPasswdField.getText, databaseNameField.getText, databasePasswdField.getText)
callbackWhenFinished()
sshNameField.getScene.getWindow.asInstanceOf[Stage].close()
}
} catch {
case ex: SQLException =>
Logger.getLogger("LoginContollerLogger").log(Level.SEVERE, null, ex) //TODO Loggername durch Klassennamen ersetzen
nameOrPasswdWrong.setVisible(true)
}
}
private def checkInputOfTextFields() {
var anyMissingInput: Boolean = false
for (t: TextField <- textFields) {
if (t.getText.isEmpty) {
anyMissingInput = true
t.getStyleClass.add("missingInput")
} else {
t.getStyleClass.remove("missingInput")
}
missingInputLabel.setVisible(anyMissingInput)
}
}
override def initialize(location: URL, resources: ResourceBundle) {
nameOrPasswdWrong.setVisible(false)
missingInputLabel.setVisible(false) //PROBLEM: NullPointerException
}
}
告诉我什么时候我错过了什么或者你想知道其他什么并感谢你的帮助。
Greetings Tracker
Tell me when i missed something or you want to know something else and thank you for your help.
Greetings Tracker
推荐答案
我找到了解决方法。
当我将missingInputLabel声明为public时,它工作正常。但实际上我不想因为信息隐藏而宣布公开。
I found a workaround.When I declare missingInputLabel as public it works just fine. But actually I don´t want to declare it public because of "information hiding".
这篇关于Scala - 获取FXML UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!