java - execute an action from a javafx menu item and keeping gui and action in separate files -
who: work county ambulance service. have forty ambulances , 2 hundred fire apparatus.
what: asking write compute program in java. have gui constructed in javafx scene builder 2.0. using netbeans code application. enclosed simple code gui. question in 2 parts; question 1.) have analysis menu expected call volume menu item. have code reaches database , pulls out information , generates poisson frequency distribution. how marry 2 things together. in other words, when user clicks menu item should fire off code. how attach code menu item? question 2.) perhaps not question, keeping action code in separate .java file. reason doing keep separate gui code. think more manageable have pieces unique things. there special need tie separate java file gui? java file action event go (fxmldocumentexplorer.java or javafxapplication2.java)?
when: hoping accomplished before 9 october 2015
where: javafx scene builder 2.0 , netbeans 8.02 on windows machine. jdk 8.
why: have lot of database code built understanding ambulance deployment. package in software package.
/*when user clicks menu item expected call volume code fires off*/ import java.lang.math; public class poissonexperiment1 { public static void main(string[] args) { /*this example of code. keeping easy. actual poisson calculations , jdbc 200 lines of ugly code.*/ double c = 7.0; int k = 1; while (k <= 15) { int factorialresult = 1; (int = 1; <= k; i++) { factorialresult = factorialresult * i; } double term1 = (math.pow(math.e, -c)); double term2 = math.pow(c, k); double numerator = term1 * term2; double answer = numerator / factorialresult; system.out.format("%10.3f:%n ", answer); k++; } } }
netbeans generates following code (fxmldocumentcontroller)
package javafxapplication2; import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.label; public class fxmldocumentcontroller implements initializable { @fxml private label label; @fxml private void handlebuttonaction(actionevent event) { system.out.println("you clicked me!"); label.settext("hello world!"); } @override public void initialize(url url, resourcebundle rb) { } }
netbeans generates following code (javafxapplication2)
package javafxapplication2; import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage; public class javafxapplication2 extends application { @override public void start(stage stage) throws exception { parent root = fxmlloader.load(getclass().getresource("fxmldocument.fxml")); scene scene = new scene(root); stage.setscene(scene); stage.show(); } public static void main(string[] args) { launch(args); } }
javafx scene builder generates gui xml document. doesn't copy , paste well.
<?xml version="1.0" encoding="utf-8"?> <?import javafx.geometry.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <anchorpane id="anchorpane" maxheight="1.7976931348623157e308" maxwidth="1.7976931348623157e308" prefheight="247.0" prefwidth="419.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.fxmldocumentcontroller"> <children> <hbox alignment="center" fillheight="false" layoutx="4.0" layouty="50.0" maxheight="1.7976931348623157e308" maxwidth="1.7976931348623157e308" minheight="-infinity" minwidth="-infinity" onmouseclicked="#handlebuttonaction" prefheight="192.0" prefwidth="411.0" style="-fx-background-color: floralwhite;" anchorpane.bottomanchor="5.0" anchorpane.leftanchor="4.0" anchorpane.rightanchor="4.0" anchorpane.topanchor="50.0"> <padding> <insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </padding> </hbox> <pane layoutx="7.0" maxheight="1.7976931348623157e308" maxwidth="1.7976931348623157e308" prefheight="247.0" prefwidth="419.0" anchorpane.bottomanchor="0.0" anchorpane.leftanchor="0.0" anchorpane.rightanchor="0.0" anchorpane.topanchor="0.0"> <children> <menubar layoutx="14.0" layouty="10.0"> <menus> <menu mnemonicparsing="false" text="file"> <items> <menuitem mnemonicparsing="false" text="close" /> </items> </menu> <menu mnemonicparsing="false" text="analysis"> <items> <menuitem fx:id="mnucomparetimeframes" mnemonicparsing="false" text="expected call volume" /> <menuitem fx:id="mnucopcncompliance" mnemonicparsing="false" text="copcn compliance" /> <menuitem fx:id="mnuresponsetimes" mnemonicparsing="false" onaction="#handlebuttonaction" text="response times" /> </items> </menu> </menus> </menubar> </children> </pane> </children> </anchorpane>
so how put these pieces together? once figure out how fire off code after user clicks on menu item, can start build rest of program.
to marry menu , function stuff.
add code controller class:
@fxml private void handlemenuaction(actionevent event) { // stuff if menu clicked }
you should use unique name can recognize if have lot of actions rather handlemenuaction
.
compile.
double click on .fxml file in netbeans should open scene builder.
select menu item want users click something.
on right side expand code
tab , use drop-down combo box on on action
field , select function added. if have many actions type or paste name in.
in scene builder save .fxml file.
in netbeans, select .fxml file in project tab , right click select make controller ( make sure .java file date .fxml file)
the code put function should called when user clicks menu item.
for question 2, code needs in controller class 1 line of code calls method in other class.
Comments
Post a Comment