Javafx tableview not showing data in all columns -
ok, new java several weeks, have been programming 30 years. following code executes, first column showing anything. data object showing multiple rows of data, fields of data filled in. i'm sure i'm missing something, , have looked through similar questions on here.
apvoucher_batchgridcontroller.java
import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.initializable; import javafx.fxml.fxml; import javafx.scene.control.tableview; import javafx.scene.input.mouseevent; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.logging.level; import java.util.logging.logger; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.scene.control.tablecolumn; import javafx.scene.control.cell.propertyvaluefactory; /** * fxml controller class * * @author kmitchell */ public class apvoucher_batchgridcontroller implements initializable { public tableview tblmainlist; public tablecolumn coldateentered; public tablecolumn colcreatedby; public tablecolumn coldescription; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { } @fxml public void opentables(actionevent event) { object forname = null; connection conn = null; statement stmt = null; resultset rs = null; coldateentered.setcellvaluefactory(new propertyvaluefactory<sresult, string>("dateentered")); coldescription.setcellvaluefactory(new propertyvaluefactory<sresult, string>("cdesc")); colcreatedby.setcellvaluefactory(new propertyvaluefactory<sresult, string>("createdby")); try { // load driver memory forname = class.forname("jstels.jdbc.dbf.dbfdriver2"); } catch (classnotfoundexception ex) { logger.getlogger(apvoucher_batchgridcontroller.class.getname()).log(level.severe, null, ex); } try { conn = drivermanager.getconnection("jdbc:jstels:dbf:e:\\keystone-data\\keyfund\\seymour\\keyfund.dbc"); } catch (sqlexception ex) { logger.getlogger(apvoucher_batchgridcontroller.class.getname()).log(level.severe, null, ex); } if (conn != null) { try { stmt = conn.createstatement(); } catch (sqlexception ex) { logger.getlogger(apvoucher_batchgridcontroller.class.getname()).log(level.severe, null, ex); } if (stmt != null) { // execute query try { observablelist<object> data = fxcollections.observablearraylist(); rs = stmt.executequery("select denteredon, cdesc, ccreatedby apvbatch ldeleted = false order denteredon desc"); while (rs.next()) { string enteredon = rs.getstring("denteredon"); string desc = rs.getstring("cdesc"); string createdby = rs.getstring("ccreatedby"); sresult row = new sresult(createdby, enteredon, desc); data.add(row); } tblmainlist.setitems(data); tblmainlist.setvisible(true); } catch (sqlexception ex) { logger.getlogger(apvoucher_batchgridcontroller.class.getname()).log(level.severe, null, ex); } } } } public class sresult { private string dateentered; private string enteredby; private string cdesc; public sresult(string t, string d, string c) { this.enteredby = t; this.dateentered = d; this.cdesc = c; } public string getenteredby() { return enteredby; } public void setenteredby(string t) { enteredby = t; } public string getdateentered() { return dateentered; } public void setdateentered(string d) { dateentered = d; } public string getcdesc() { return cdesc; } public void setcdesc(string c) { cdesc = c; } } }
and apvoucher_batchgrid.fxml
<?xml version="1.0" encoding="utf-8"?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.geometry.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <anchorpane id="anchorpane" fx:id="batchlistform" prefheight="400.0" prefwidth="600.0" styleclass="mainfxmlclass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="keystone.apvoucher_batchgridcontroller"> <children> <borderpane layoutx="0.0" layouty="0.0" prefheight="400.0" prefwidth="600.0"> <center> <anchorpane prefheight="-1.0" prefwidth="-1.0"> <children> <pane layoutx="0.0" layouty="0.0" prefheight="53.0" prefwidth="580.0"> <children> <label layoutx="7.0" layouty="9.0" prefwidth="202.0" text="ap vouchers batch list"> <font> <font name="system bold" size="14.0" /> </font> </label> <button fx:id="btnclose" cancelbutton="true" layoutx="513.0" layouty="27.0" mnemonicparsing="false" text="close" /> <button id="btnclose" fx:id="apvrefresh" cancelbutton="true" layoutx="185.0" layouty="27.0" mnemonicparsing="false" onaction="#opentables" text="refresh" /> </children> </pane> <tableview fx:id="tblmainlist" layoutx="0.0" layouty="53.0" prefheight="323.0" prefwidth="580.0"> <columns> <tablecolumn maxwidth="5000.0" minwidth="10.0" prefwidth="91.0" text="date entered" fx:id="coldateentered" /> <tablecolumn maxwidth="5000.0" minwidth="10.0" prefwidth="100.0" text="created by" fx:id="colcreatedby" /> <tablecolumn maxwidth="5000.0" minwidth="10.0" prefwidth="261.0" text="description" fx:id="coldescription" /> </columns> </tableview> </children> </anchorpane> </center> <padding> <insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> </padding> </borderpane> </children> <stylesheets> <url value="@apvoucher_batchgrid.css" /> </stylesheets> </anchorpane>
thank answer. way many years in case insensitive languages. has been quick , dirty exercise me learn java , latest & greatest stuff or new exciting technology (next!)
for looking @ answer , still not clued in, here changes made code work properly.
coldateentered.setcellvaluefactory(new propertyvaluefactory<sresult, string>("denteredon")); coldescription.setcellvaluefactory(new propertyvaluefactory<sresult, string>("cdesc")); colenteredby.setcellvaluefactory(new propertyvaluefactory<sresult, string>("ccreatedby")); public class sresult { private string denteredon; private string ccreatedby; private string cdesc; public sresult(string t, string d, string c) { this.ccreatedby = t; this.denteredon = d; this.cdesc = c; } public string getccreatedby() { return ccreatedby; } public void setcreatedby(string t) { ccreatedby = t; } public string getdenteredon() { return denteredon; } public void setdenteredon(string d) { denteredon = d; } public string getcdesc() { return cdesc; } public void setcdesc(string c) { cdesc = c; } } }
this question duplicate of: javafx propertyvaluefactory not populating tableview, i'll address specific case, it's clear.
background
propertyvaluefactory uses reflection determine methods , set data values retrieve bindable properties model class. pattern followed is:
propertyvaluetype getname() void setname(propertyvaluetype value) propertytype nameproperty()
where "name" string specified in propertyvaluefactory constructor. first letter of property name in getter , setter capitalized (by java bean naming convention).
why application doesn't work
you have these 3 expressions:
new propertyvaluefactory<sresult, string>("dateentered") new propertyvaluefactory<sresult, string>("cdesc") new propertyvaluefactory<sresult, string>("createdby")
for sample properties, propertyvaluefactory these methods:
"dateentered" => getdateentered() "cdesc" => getcdesc() "createdby" => getcreatedby()
and have these 3 getters on sresult
class:
getdateentered() getcdesc() getenteredby()
only getdateentered()
going picked propertyvaluefactory because matching method defined in sresult
class.
advice
you have adopt java standards if want reflection in propertyvaluefactory work (the alternative not use propertyvaluefactory , instead write own cell factories scratch).
adopting java camel case naming conventions makes easier java developers read code.
Comments
Post a Comment