Skip to content

Instantly share code, notes, and snippets.

@sundararajana
Created January 6, 2015 04:35
Show Gist options
  • Save sundararajana/1fbe15e8a7dc7f195328 to your computer and use it in GitHub Desktop.
Save sundararajana/1fbe15e8a7dc7f195328 to your computer and use it in GitHub Desktop.
Beginner #javafx #nashorn calculator
/**
* This Nashorn script is port of "Hello Math" JavaFX example
* at http://www.javafxtutorials.com/javafxsimplecode/
*
* Ported by: https://twitter.com/sundararajan_a
*
* "mindless" port with these changes:
*
* Java imports -> Java.type
* Bean style getter/setter -> property access style
* Java closures -> script functions
*/
// JavaFX classes referred here
var Button = Java.type("javafx.scene.control.Button");
var GridPane = Java.type("javafx.scene.layout.GridPane");
var Label = Java.type("javafx.scene.control.Label");
var Pos = Java.type("javafx.geometry.Pos");
var Scene = Java.type("javafx.scene.Scene");
var TextField = Java.type("javafx.scene.control.TextField");
// variables to hold JavaFX UI control objects
var txtnum1, txtnum2;
var btnadd, btnsub, btndiv, btnmul, btnclear;
var lblanswer;
function start(primaryStage) {
//make the controls
txtnum1 = new TextField();
txtnum2 = new TextField();
btnadd = new Button("+");
btnsub = new Button("-");
btnmul = new Button("x");
btndiv = new Button("/");
btnclear = new Button("Clear");
lblanswer = new Label("?");
//center text in label
lblanswer.alignment = Pos.CENTER;
//apply ccs-like style to label (yes, you can)
lblanswer.style = "-fx-border-color: #000; -fx-padding: 5px;";
//make container for app
var root = new GridPane();
//put container in middle of scene
root.alignment = Pos.CENTER;
//setspacing between controls in grid
root.hgap = 10;
root.vgap = 10;
//add to grid, cell by cell
root.add(btnadd,0,0);
root.add(btnsub,1,0);
root.add(btnmul,0,1);
root.add(btndiv,1,1);
root.add(txtnum1, 0,2);
root.add(txtnum2,1,2);
//last 2 rows span across 2 columns
//col, rol, colspan, rowspan
root.add(lblanswer,0,3,2,1);
root.add(btnclear,0,4,2,1);
//set widths of all controls in separate method
setWidths();
//attach buttons to code in separate method
attachCode();
//usual stuff
var scene = new Scene(root, 300, 250);
primaryStage.title = "Mathtastic 1.0";
primaryStage.scene = scene;
primaryStage.show();
}
function setWidths(){
//set widths of all controls
txtnum1.prefWidth = 70;
txtnum2.prefWidth = 70;
btnadd.prefWidth = 70;
btnsub.prefWidth = 70;
btnmul.prefWidth = 70;
btndiv.prefWidth = 70;
btnclear.prefWidth = 150;
lblanswer.prefWidth = 150;
}
function attachCode() {
//have each button run BTNCODE when clicked
btnadd.onAction = btncode;
btnsub.onAction = btncode;
btnmul.onAction = btncode;
btndiv.onAction = btncode;
btnclear.onAction = btncode;
}
function btncode(e) {
var num1, num2, answer;
var symbol;
//e tells us which button was clicked
if (e.source == btnclear) {
txtnum1.text = "";
txtnum2.text = "";
lblanswer.text = "?";
txtnum1.requestFocus();
return;
}
//read numbers in from textfields
num1 = parseInt(txtnum1.text);
num2 = parseInt(txtnum2.text);
if (e.source == btnadd) {
symbol = '+';
answer = num1 + num2;
} else if (e.source == btnsub) {
symbol = '-';
answer = num1 - num2;
} else if (e.source == btnmul) {
symbol = 'x';
answer = num1 * num2;
} else {
symbol = '/';
answer = num1 / num2;
}
//display answer
lblanswer.text = "" + num1 + symbol + num2 + "=" + answer;
}
@sundararajana
Copy link
Author

To run that script, use

jjs -fx calc.js

See also jjs man page at http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jjs.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment