java - Javafx - How to Add Pie Chart to BorderPane from Different Class -
i have 2 classes. main class , chart class. chart class extends piechart , instantiates pie chart in constructor. main class creates borderpane, instantiate object of chart class(creating pie chart) , add center pane.the problem don't know exact syntax make work properly, in constructor of chart class.
i know example, if creating hbox instead of pie chart in constructor, use this. , number of methods. pie charts have additional elements observable lists i'm not sure how make work? here 2 classes below. please help. thank you.
main class
public class main extends application { @override public void start(stage primarystage) { borderpane border = new borderpane(); chart chart = new chart(); border.setcenter(chart); scene scene = new scene (border, 640,680); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); }}
chart class
public class chart extends piechart{ public chart(){ observablelist<piechart.data>piechart = fxcollections.observablearraylist( new piechart.data("fruits", 75), new piechart.data("vegetables", 25) ); piechart chart = new piechart(piechart); //what code can create pie chart here? solution place chart on it's own pane instead? }}
you looking
public chart(){ observablelist<piechart.data>piechart = fxcollections.observablearraylist( new piechart.data("fruits", 75), new piechart.data("vegetables", 25) ); setdata(piechart); // no need -> piechart chart = new piechart(piechart); }
or
public chart(){ super(fxcollections.observablearraylist( new piechart.data("fruits", 75), new piechart.data("vegetables", 25) )); }
edit after comment: setdata(observablelist data) method piechart class. , set data objects. easy , convenient way work data in chart or other collection powered view. (yep, not chart-only style. pretty same javafx views. chart listen data changes , redraw internaly.you need set data you'd display. in case have method because chart extends piechart
when piechart charts = new piechart(data);
in piechart call setdata internaly
public piechart(observablelist<piechart.data> data){ setdata(data); ... }
so that's why have call super(data) or setdata(data) in extended class. if want chage charts, don't have recreate whole chart every time. can change data charts.setdata(somedata)
or data list object charts.getdata()
, change it.
for example:
piechart charts = new piechart(); observablelist<piechart.data> piechart = ... charts.setdata(piechart); // piechart == charts.getdata() // same object. pinechart.add(new piechart.data("new fruit", 75)) // change chart
btw, take @ tutorials https://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm
Comments
Post a Comment