Sunday, 15 March 2015

java - Accessing TextField from a separate class -



java - Accessing TextField from a separate class -

i attempting create very basic class mycontroller, takes content of textfield in fxml created window and, when button in window pressed, inserts contents string printed terminal. 1 time done, changes contents of textfield "done!".

the code fxml file follows:

<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <anchorpane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="practice1.mycontroller"> <children> <button fx:id="helloworld" layoutx="30.0" layouty="34.0" mnemonicparsing="false" onaction="#sayhello" text="hello world" /> <textfield fx:id="message" layoutx="30.0" layouty="77.0" /> </children> </anchorpane>

the info of involvement in textfield has fx:id "message".

the code class mycontroller follows:

package practice1; import javafx.event.*; import javafx.scene.control.textfield; public class mycontroller { private textfield message; public void sayhello(actionevent event) { system.out.print("you said \""); system.out.print(message.gettext()); system.out.println("\""); message.settext("done!"); } }

your textfield in controller private loader cannot access it. using javafx.fxml.fxml annotation follows can mark accessible fxml.

@fxml private textfield message;

you alternatively declare public.

java fxml scenebuilder

No comments:

Post a Comment