javafx - Scala - Get FXML UI-Elements -
i´m working on scala/javafx project intellij , plugin scala.
typical way in java access elements of fxml-file set id each element want access , in controllerclass declare variable "@fxml private label mylabelid;".
in scala works same way can see in source code. nullpointerexception in line 73 (marked problem-comment). label, set null. tried diffrent things. every single element getting set expected apart 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:
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 set 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: close gtevconnection def onkeyreleased_textfield(ev: keyevent) { //todo declare def private , mark @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 (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 } }
tell me when missed or want know else , thank help.
greetings tracker
i found workaround. when declare missinginputlabel public works fine. don´t want declare public because of "information hiding".
Comments
Post a Comment