java - javafx tableview how to get selected data without using class -
i working on javafx project , want selected data tableview don't want use class because number of columns not fixed .so, please me.
for example:
public static class identifiedname { private final int id; private final string name; private identifiedname(int id, string name) { this.id = id; this.name = name; } public int getid() { return id; } public string getname() { return name; } }
this class can use 2 column table , in project client can add column per need please me selected data tableview
more code here:
import java.sql.connection; import java.sql.resultset; import java.util.map; import javafx.application.application; import javafx.beans.property.simplebooleanproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.checkbox; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celldatafeatures; import javafx.scene.control.tableposition; import javafx.scene.control.tableview; import javafx.scene.control.tableview.tableviewselectionmodel; import javafx.scene.control.cell.textfieldtablecell; import javafx.stage.stage; import javafx.util.callback; import javafx.util.stringconverter; public class dynamictable extends application{ object newvalue; private observablelist<observablelist> data; private tableview<observablelist> tableview; public static void main(string[] args) { launch(args); } //connection database public void builddata(){ connection c ; data = fxcollections.observablearraylist(); try{ c = dbconnect.connect(); //sql selecting of customer string sql = "select * customer"; //resultset resultset rs = c.createstatement().executequery(sql); /********************************** * table column added dynamically * **********************************/ for(int i=0 ; i<rs.getmetadata().getcolumncount(); i++){ //we using non property style making dynamic table final int j = i; tablecolumn col = new tablecolumn(rs.getmetadata().getcolumnname(i+1)); col.setcellvaluefactory(new callback<celldatafeatures<observablelist,string>,observablevalue<string>>(){ public observablevalue<string> call(celldatafeatures<observablelist, string> param) { return new simplestringproperty(param.getvalue().get(j).tostring()); } }); tableview.getcolumns().addall(col); tableview.seteditable(true); callback<tablecolumn<map, string>, tablecell<map, string>> cellfactoryformap = new callback<tablecolumn<map, string>, tablecell<map, string>>() { @override public tablecell call(tablecolumn p) { return new textfieldtablecell(new stringconverter() { @override public string tostring(object t) { return t.tostring(); } @override public object fromstring(string string) { return string; } }); } }; if(j!=1) col.setcellfactory(cellfactoryformap); system.out.println("column ["+i+"] "); } /******************************** * data added observablelist * ********************************/ while(rs.next()){ //iterate row observablelist<string> row = fxcollections.observablearraylist(); for(int i=1 ; i<=rs.getmetadata().getcolumncount(); i++){ //iterate column row.add(rs.getstring(i)); } system.out.println("row [1] added "+row ); data.add(row); } //finally added tableview tableview.setitems(data); }catch(exception e){ e.printstacktrace(); system.out.println("error on building data"); } } @override public void start(stage stage) throws exception { //tableview tableview = new tableview(); builddata(); //main scene scene scene = new scene(tableview); stage.setscene(scene); stage.show(); tableview.getselectionmodel().selecteditemproperty().addlistener(new changelistener() { @override public void changed(observablevalue observablevalue, object oldvalue, object newvalue) { //check whether item selected , set value of selected item label if(tableview.getselectionmodel().getselecteditem() != null) { tableviewselectionmodel selectionmodel = tableview.getselectionmodel(); observablelist selectedcells = selectionmodel.getselectedcells(); tableposition tableposition = (tableposition) selectedcells.get(0); object val = tableposition.gettablecolumn().getcelldata(newvalue); system.out.println("selected value" + val); } } }); tablecolumn col_action = new tablecolumn<>("action"); col_action.setsortable(false); col_action.setcellvaluefactory( new callback<tablecolumn.celldatafeatures<javafxdyntable2.record, boolean>, observablevalue<boolean>>() { @override public observablevalue<boolean> call(tablecolumn.celldatafeatures<javafxdyntable2.record, boolean> p) { return new simplebooleanproperty(p.getvalue() != null); } }); col_action.setcellfactory( new callback<tablecolumn<javafxdyntable2.record, boolean>, tablecell<javafxdyntable2.record, boolean>>() { @override public tablecell<javafxdyntable2.record, boolean> call(tablecolumn<javafxdyntable2.record, boolean> p) { return new buttoncell(tableview); } }); tableview.getcolumns().add(col_action); } private class buttoncell extends tablecell<javafxdyntable2.record, boolean> { final checkbox cellbutton = new checkbox(); buttoncell(final tableview tblview){ cellbutton.setonaction(new eventhandler<actionevent>(){ @override public void handle(actionevent t) { } }); } //display button if row not empty @override protected void updateitem(boolean t, boolean empty) { super.updateitem(t, empty); if(!empty){ setgraphic(cellbutton); } } } }
the problem wasn't errors, it's fix before posting code, database don't have.
i changed table uses observablelist<string>
since that's you're using , it's best specify structure in table.
this run if have h2 jar in path or set library in project. can remove part of code , use database.
package dynamictable; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; import java.util.map; import javafx.application.application; import static javafx.application.application.launch; import javafx.beans.property.simplestringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.scene.scene; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celldatafeatures; import javafx.scene.control.tableposition; import javafx.scene.control.tableview; import javafx.scene.control.tableview.tableviewselectionmodel; import javafx.scene.control.cell.textfieldtablecell; import javafx.stage.stage; import javafx.util.callback; import javafx.util.stringconverter; public class dynamictable extends application{ private observablelist<observablelist<string>> data; private tableview<observablelist<string>> tableview; //connection database public void builddata() { data = fxcollections.observablearraylist(); try { drivermanager.registerdriver(new org.h2.driver()); connection c = drivermanager.getconnection("jdbc:h2:mem:"); //or can use derby in jdk folder //drivermanager.registerdriver(new org.apache.derby.jdbc.embeddeddriver()); //connection c = drivermanager.getconnection("jdbc:derby:memory:mydb;create=true"); statement stmt = c.createstatement(); string sql = "create table customer (num integer, name varchar(255), address varchar(255))"; stmt.executeupdate(sql); (int = 0; < 10; i++) { sql = "insert customer values (" + + ",'1st string in row " + + "','2nd string in row " + + "')"; stmt.executeupdate(sql); } //c = dbconnect.connect(); //sql selecting of customer string sql = "select * customer"; //resultset resultset rs = c.createstatement().executequery(sql); /** * ******************************** * table column added dynamically * ********************************* */ (int = 0; < rs.getmetadata().getcolumncount(); i++) { //we using non property style making dynamic table final int j = i; tablecolumn col = new tablecolumn(rs.getmetadata().getcolumnname(i + 1)); col.setcellvaluefactory(new callback<celldatafeatures<observablelist<string>, string>, observablevalue<string>>() { public observablevalue<string> call(celldatafeatures<observablelist<string>, string> param) { return new simplestringproperty(param.getvalue().get(j)); } }); tableview.getcolumns().add(col); callback<tablecolumn<map, string>, tablecell<map, string>> cellfactoryformap = new callback<tablecolumn<map, string>, tablecell<map, string>>() { @override public tablecell call(tablecolumn p) { return new textfieldtablecell(new stringconverter() { @override public string tostring(object t) { return t.tostring(); } @override public object fromstring(string string) { return string; } }); } }; if (j != 1) { col.setcellfactory(cellfactoryformap); } system.out.println("column [" + + "] "); } /** * ****************************** * data added observablelist * ******************************* */ while (rs.next()) { //iterate row observablelist<string> row = fxcollections.observablearraylist(); (int = 1; <= rs.getmetadata().getcolumncount(); i++) { //iterate column row.add(rs.getstring(i)); } system.out.println("row [1] added " + row); data.add(row); } rs.close(); c.close(); //finally added tableview tableview.setitems(data); } catch (exception e) { e.printstacktrace(); system.out.println("error on building data"); } } @override public void start(stage stage) throws exception { //tableview tableview = new tableview(); tableview.seteditable(true); tableview.getselectionmodel().setcellselectionenabled(true); builddata(); //main scene scene scene = new scene(tableview); stage.setscene(scene); stage.show(); tableview.getselectionmodel().selecteditemproperty().addlistener(new changelistener() { @override public void changed(observablevalue observablevalue, object oldvalue, object newvalue) { //check whether item selected , set value of selected item label if (tableview.getselectionmodel().getselecteditem() != null) { tableviewselectionmodel selectionmodel = tableview.getselectionmodel(); observablelist selectedcells = selectionmodel.getselectedcells(); tableposition tableposition = (tableposition) selectedcells.get(0); object val = tableposition.gettablecolumn().getcelldata(newvalue); system.out.println("selected value " + val); system.out.println("selected row " + newvalue); } } }); system.out.println(""); system.out.println("list data without using table"); (observablelist<string> ol : data){ system.out.print("row-> "); (string s : ol){ system.out.print(s+" "); } system.out.println(""); } system.out.println(""); system.out.println("show data @ specific location in data"); system.out.println(" row 6 (index 5), column 2(index 1) -> "+data.get(5).get(1)); system.out.println(""); system.out.println("show data @ specific location in table"); system.out.println(" row 2, col 2 -> "+tableview.getitems().get(1).get(1)); } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment