tableview - How to access table data from outside the controller in javafx -
i'm trying create material inventory program uses tableview. have controller show statistics based on tableview i'm not sure how access live data view.
my mainapp loads last saved table , materialoverviewcontroller has tableview.
i have rootlayoutcontroller main layout controller has simple file menu onfloorstatscontroller loaded from.
i can create new mainapp when onfloorstatscontroller loaded loads sample data entered default.
how access live data mainapp?
here mainapp code:
public class mainapp extends application { private stage primarystage; private borderpane rootlayout; private observablelist<material> materialdata = fxcollections.observablearraylist(); @override public void start(stage primarystage) { this.primarystage = primarystage; this.primarystage.settitle("label inventory"); this.primarystage.geticons().add(new image("file:resources/images/1462487405_mask.png")); initrootlayout(); showmaterialoverview(); } public mainapp() { // add sample data } public observablelist<material> getmaterialdata() { return materialdata; } /** * initializes root layout , tries load last opened * material file. */ public void initrootlayout() { try { // load root layout fxml file. fxmlloader loader = new fxmlloader(); loader.setlocation(mainapp.class .getresource("rootlayout.fxml")); rootlayout = (borderpane) loader.load(); // show scene containing root layout. scene scene = new scene(rootlayout); primarystage.setscene(scene); // give controller access main app. rootlayoutcontroller controller = loader.getcontroller(); controller.setmainapp(this); primarystage.show(); } catch (ioexception e) { e.printstacktrace(); } // try load last opened person file. file file = getmaterialfilepath(); if (file != null) { loadmaterialdatafromfile(file); } } public void showmaterialoverview() { try { // load material overview. fxmlloader loader = new fxmlloader(); loader.setlocation(mainapp.class.getresource("materialoverview.fxml")); anchorpane materialoverview = (anchorpane) loader.load(); // set material overview center of root layout. rootlayout.setcenter(materialoverview); // give controller access main app. materialoverviewcontroller controller = loader.getcontroller(); controller.setmainapp(this); } catch (ioexception e) { e.printstacktrace(); } } public void showonfloorstatistics() { try { // load fxml file , create new stage popup. fxmlloader loader = new fxmlloader(); loader.setlocation(mainapp.class.getresource("onfloorstats.fxml")); anchorpane page = (anchorpane) loader.load(); stage dialogstage = new stage(); dialogstage.settitle("material on floor"); dialogstage.initmodality(modality.window_modal); dialogstage.initowner(primarystage); scene scene = new scene(page); dialogstage.setscene(scene); // set material controller. onfloorstatscontroller controller = loader.getcontroller(); controller.start(dialogstage); dialogstage.show(); } catch (ioexception e) { e.printstacktrace(); } } public void showbarcharttest() { try { // load fxml file , create new stage popup. fxmlloader loader = new fxmlloader(); loader.setlocation(mainapp.class.getresource("barcharttestlayout.fxml")); anchorpane page = (anchorpane) loader.load(); stage dialogstage = new stage(); dialogstage.settitle("detailed bar chart"); // dialogstage.initmodality(modality.window_modal); dialogstage.initowner(primarystage); scene scene = new scene(page); dialogstage.setscene(scene); // set material controller. barcharttest controller = loader.getcontroller(); controller.start(dialogstage); dialogstage.show(); } catch (ioexception e) { e.printstacktrace(); } } /** * returns main stage. * @return */ public stage getprimarystage() { return primarystage; } public static void main(string[] args) { launch(args); } }
and here materialoverviewcontroller code:
public class materialoverviewcontroller { @fxml private tableview<material> materialtable; private list<material> materialid; public tableview<material> getmaterialtable() { return materialtable; } // public void setmaterialtable(tableview<material> materialtable) { // this.materialtable = materialtable; } private observablelist<integer> rollstotal = fxcollections.observablearraylist(); private observablelist<string> materialnames = fxcollections.observablearraylist(); // reference main application. private mainapp mainapp; /** * constructor. * constructor called before initialize() method. */ public materialoverviewcontroller() { } /** * initializes controller class. method automatically called * after fxml file has been loaded. */ @fxml private void initialize() { materialtotal = 0; // initialize material table columns. ponumbercolumn.setcellvaluefactory(celldata -> celldata.getvalue().ponumberproperty()); materialnamecolumn.setcellvaluefactory(celldata -> celldata.getvalue().materialnameproperty()); materialidcolumn.setcellvaluefactory(celldata -> celldata.getvalue().materialidproperty()); materialwidthcolumn.setcellvaluefactory(celldata -> celldata.getvalue().materialwidthproperty().asobject()); qtyorderedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().qtyorderedproperty().asobject()); qtyreceivedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().qtyreceivedproperty().asobject()); qtybackorderedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().qtybackorderedproperty().asobject()); // qtybackorderedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().qtybackorderedproperty().asobject()); qtyonfloorcolumn.setcellvaluefactory(celldata -> celldata.getvalue().qtyonfloorproperty().asobject()); // dateorderedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().dateorderedproperty().asstring()); dateorderedcolumn.setcellvaluefactory(celldata -> celldata.getvalue().dateorderedproperty().asstring()); estarrivalcolumn.setcellvaluefactory(celldata -> celldata.getvalue().dateexpectedproperty().asstring()); suppliercolumn.setcellvaluefactory(celldata -> celldata.getvalue().supplierproperty()); // clear material details. showmaterialdetails(null); // listen selection changes , show material details when changed. materialtable.getselectionmodel().selecteditemproperty().addlistener( (observable, oldvalue, newvalue) -> showmaterialdetails(newvalue)); } public void setmainapp(mainapp mainapp) { this.mainapp = mainapp; // add observable list data table materialtable.setitems(mainapp.getmaterialdata()); } private void showmaterialdetails(material material) { if (material != null) { // fill labels info material object. ponumberlabel.settext(material.getponumber()); materialidlabel.settext(material.getmaterialid()); materialnamelabel.settext(material.getmaterialname()); materialwidthlabel.settext(string.valueof(material.getmaterialwidth())); qtyorderedlabel.settext(integer.tostring(material.getqtyordered())); qtyrecievedlabel.settext(integer.tostring(material.getqtyreceived())); qtybackorderedlabel.settext(integer.tostring(material.getqtyordered()-material.getqtyreceived())); // qtybackorderedlabel.settext(integer.tostring(material.getqtybackordered())); qtyonfloorlabel.settext(integer.tostring(material.getqtyonfloor())); // dateorderedlabel.settext(dateutil.format(material.getdateordered())); // dateorderedlabel.equals(dateutil.format(material.getdateordered())); // dateorderedlabel.settext(datepicker(string.valueof(material.getdateordered()); // estarrivallabel.settext(material.getsupplier()); supplierlabel.settext(material.getsupplier()); string pattern = "mm/dd/yyyy"; // datetesttextfield.settext(dateutil.format(material.getbirthday())); dateorderedlabel.settext(dateutil.format(material.getdateordered())); estarrivallabel.settext(dateutil.format(material.getdateexpected())); //todo: set running material totals // getmaterialnames(arrays.aslist(material)); handleselectmaterial(); } } } public list getmaterialnames(list<material> materials) { (material name : materials) { mainapp.getmaterialdata(); if (!materialnames.contains(name)) { materialnames.add(name.getmaterialname()); } } (string name : materialnames) { materialtextfield.settext(name + "\n"); } return arrays.aslist(materialnames); } } }
here bit of code works rootlayoutcontroller i'm not sure why can't same thing onfloorstatscontroller:
public void setmainapp(mainapp mainapp) { this.mainapp = mainapp; } @fxml private void handleprint() { fxmlloader fxmlloader = new fxmlloader(); fxmlloader.setlocation(getclass().getresource("materialoverview.fxml")); try { anchorpane frame = fxmlloader.load(); } catch (ioexception e) { e.printstacktrace(); } materialoverviewcontroller c = fxmlloader.getcontroller(); //loads data table c.setmainapp(mainapp);
i need access live data materialovewviewcontroller
here's onfloorstatscontroller:
private mainapp mainapp; public tableview<material> material; /** * called main application give reference itself. * * @param mainapp */ public void setmainapp(mainapp mainapp) { this.mainapp = mainapp; } // final static string austria = "14532"; // final static string brazil = "78333"; // final static string france = "40443"; // final static string italy = "72825"; // final static string usa = "72829"; public void start(stage stage) { fxmlloader fxmlloader = new fxmlloader(); fxmlloader.setlocation(getclass().getresource("materialoverview.fxml")); try { anchorpane frame = fxmlloader.load(); } catch (ioexception e) { e.printstacktrace(); } materialoverviewcontroller c = fxmlloader.getcontroller(); //loads data table c.setmainapp(mainapp); stage.settitle("material on floor"); final categoryaxis xaxis = new categoryaxis(); final numberaxis yaxis = new numberaxis(); final barchart<string, number> bc = new barchart<string, number>(xaxis, yaxis); bc.settitle("material on floor"); xaxis.setlabel("material"); yaxis.setlabel("quantity"); xychart.series series1 = new xychart.series(); series1.setname("6 inch"); integer floortotal = 0; (material id :mainapp.getmaterialdata()) { if (id.getmaterialwidth() < 6.9) { floortotal = id.getqtyonfloor(); (material size : mainapp.getmaterialdata()) { if ( id.getmaterialid().equalsignorecase(size.getmaterialid()) && size.getmaterialwidth() == id.getmaterialwidth()) { floortotal += size.getqtyonfloor(); } else { floortotal = id.getqtyonfloor(); }
Comments
Post a Comment