java - Is it possible to use a specific class in the javafx Dialog? -
i searching javafx joptionpane equivalent , did found great class dialog. in tutorial, tutor used : dialog<pair<string,string>> 2 string input fields , here m wondering possible use class let : dialog<product>. if possible how should write class specific pattern or plot so? thanks
yes, can that. answer basing on:
https://examples.javacodegeeks.com/desktop-java/javafx/dialog-javafx/javafx-dialog-example/
assuming product has 2 fields can passed via constructor:
string name; float price; you can create dialog in such way:
dialog<product> dialog = new dialog<>(); dialog.settitle("product dialog"); dialog.setresizable(true); label namelabel = new label("name: "); label pricelabel = new label("price: "); textfield namefield = new textfield(); textfield pricefield = new textfield(); gridpane grid = new gridpane(); grid.add(namelabel, 1, 1); grid.add(namefield, 2, 1); grid.add(pricelabel, 1, 2); grid.add(pricefield, 2, 2); dialog.getdialogpane().setcontent(grid); buttontype savebutton = new buttontype("save", buttondata.ok_done); dialog.getdialogpane().getbuttontypes().add(savebutton); dialog.setresultconverter(new callback<buttontype, product>() { @override public product call(buttontype button) { if (button == savebutton) { string name = namefield.gettext(); float price; try { price = float.parsefloat(pricefield.gettext()); } catch (numberformatexception e) { // add log or inform user wrong price return null; } return new product(name, price); } return null; } }); optional<product> result = dialog.showandwait(); if (result.ispresent()) { product product = result.get(); // product }
Comments
Post a Comment