java - tableview not showing data from database -
i unable populate tableview. believe problem in controller, in way data being sent fxml file, because system out print (see below) shows have in database.
please let me know did mistake. went on tutorials exist on that, nothing fits problem.
thanks
main app:
package tableview; import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage; public class mainapp extends application { public static void main(string[] args) { // todo auto-generated method stub launch(args); } @override public void start(stage primarystage) throws exception { parent root = fxmlloader.load(getclass().getresource("view/fxmltable.fxml")); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); } }
controller:
import tableview.model.person; public class fxmltablecontroller{ @fxml public tableview<person> tableview ; @fxml private tablecolumn<person, number> clientidcolumn; @fxml private tablecolumn<person, string> firstnamecolumn; @fxml private tablecolumn<person, string> lastnamecolumn; @fxml private void initialize() { assert tableview != null : "fx:id=\"tableview\" not injected: check fxml file 'usermaster.fxml'."; clientidcolumn.setcellvaluefactory(celldata -> celldata.getvalue(). clientidproperty()); firstnamecolumn.setcellvaluefactory(celldata -> celldata.getvalue() .firstnameproperty()); lastnamecolumn.setcellvaluefactory(celldata -> celldata.getvalue() .lastnameproperty()); builddata(); } private observablelist<person> data; public void builddata(){ data = fxcollections.observablearraylist(); connection con = null; try { class.forname("org.sqlite.jdbc"); con = drivermanager.getconnection("jdbc:sqlite:tableviewdb.db"); string sql = "select * info"; resultset rs = con.createstatement().executequery(sql); while(rs.next()){ person per = new person(); per.clientid.set(rs.getint("clientid")); per.firstname.set(rs.getstring("firstname")); per.lastname.set(rs.getstring("lastname")); data.add(per); } tableview = new tableview<person>(); tableview.setitems(data); system.out.println(tableview.getitems().get(1).clientid); } catch(exception e){ e.printstacktrace(); system.out.println("error on building data"); } } }
model class:
package tableview.model; import javafx.beans.property.integerproperty; import javafx.beans.property.simpleintegerproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; public class person { public simpleintegerproperty clientid = new simpleintegerproperty(); public simplestringproperty firstname = new simplestringproperty(); public simplestringproperty lastname = new simplestringproperty(); public simpleintegerproperty getclientid() { return clientid; } public simplestringproperty getfirstname() { return firstname; } public simplestringproperty getlastname() { return lastname; } public integerproperty clientidproperty(){ return clientid; } public stringproperty firstnameproperty(){ return firstname; } public stringproperty lastnameproperty(){ return lastname; } }
fxml file: (disregard save button now...)
<?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?> <anchorpane prefheight="400.0" prefwidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> <children> <splitpane prefheight="400.0" prefwidth="600.0"> <items> <splitpane dividerpositions="0.5" orientation="vertical" prefheight="200.0" prefwidth="160.0"> <items> <anchorpane minheight="0.0" minwidth="0.0" prefheight="100.0" prefwidth="160.0"> <children> <textfield layoutx="93.0" layouty="34.0" /> <textfield layoutx="93.0" layouty="85.0" /> <label layoutx="35.0" layouty="39.0" text="name" /> <label layoutx="35.0" layouty="90.0" text="email" /> <button layoutx="204.0" layouty="140.0" mnemonicparsing="false" text="save" /> </children> </anchorpane> <anchorpane minheight="0.0" minwidth="0.0" prefheight="100.0" prefwidth="160.0"> <children> <tableview layouty="-2.0" prefheight="200.0" prefwidth="598.0"> <columns> <tablecolumn prefwidth="302.0" text="name" /> <tablecolumn prefwidth="295.0" text="email" /> </columns> </tableview> </children> </anchorpane> </items> </splitpane> </items> </splitpane> </children> </anchorpane>
you're creating new tableview
, setting items, instead of setting items on table fxml file defined. remove
tableview = new tableview<person>();
from controller.
to @fxml
-annotated fields in controller populated appropriate elements fxml file, need add fx:id
attributes elements:
<tableview fx:id="tableview" ... > <columns> <tablecolumn fx:id="firstnamecolumn" ... /> ...
Comments
Post a Comment