java - Populate combo box list dynamically for each row in javaFx table view -
i have created table view in java fx, 1 of columns in table view consists of comboboxtablecell.please find below code table view
public class tabviewcontroller { @fxml private tabpane cnfmtab; @fxml private tableview<tabvo> partstable; @fxml private tablecolumn<tabvo, string> column1; @fxml private tablecolumn<tabvo, string> column2; @fxml private tablecolumn<tabvo, string> column3; private observablelist<tabvo> tabledata = fxcollections.observablearraylist(); private observablelist<string> column1list; @fxml public void initialize(){ tabledata = fxcollections.observablearraylist(calldao.gettabledata(1)); column1.setcellvaluefactory(celldata -> new simplestringproperty(celldata.getvalue().getcolumn1())); column1 .setcellfactory(new callback<tablecolumn<tabvo, string>, tablecell<tabvo, string>>() { @suppresswarnings({ "rawtypes", "unchecked" }) public tablecell call(tablecolumn p) { comboboxtablecell cell = new comboboxtablecell(column1list){ @override public void updateitem(object item, boolean empty) { } }; cell.setalignment(pos.center); return cell; } }); column1.setoneditcommit( new eventhandler<celleditevent<tabvo, string>>() { @override public void handle(celleditevent<tabvo,string> t) { }; } ); } partstable.setitems(tabledata); }
data object:
public class tabvo { private string column1; private string column2; private string column3; private observablelist<string> column1list; /* getters , setters */ }
in above code each row of table of type tabvo.
in table column1 of type combo box. right list column1 combo box populate 'column1list' declared in 'tabviewcontroller', because of rows of table view populated same drop down.
but want each row of table populated different list present in tabvo.
can please tell me how access tabvo object , populate combobox list present in tabvo?
this mvce use case described in question:
import javafx.application.application; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.eventhandler; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.control.combobox; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.control.cell.textfieldtablecell; import javafx.scene.layout.hbox; import javafx.stage.stage; import javafx.util.callback; public class comboboxcelltableexample extends application { private final tableview<tabvo> table = new tableview<>(); private final observablelist<tabvo> data = fxcollections.observablearraylist( new tabvo( "2222", "column2", fxcollections.<string>observablearraylist( "111", "2222", "3333" ) ), new tabvo( "bbbb", "column2", fxcollections.<string>observablearraylist( "aaaa", "bbbb", "ccccc" ) ), new tabvo( "6666", "column2", fxcollections.<string>observablearraylist( "444", "5555", "6666" ) ), new tabvo( "7777", "column2", fxcollections.<string>observablearraylist( "7777", "8888", "99999" ) ), new tabvo( "hhhh", "column2", fxcollections.<string>observablearraylist( "hhhh", "jjjj", "kkkkk" ) ) ); @override public void start( stage stage ) { scene scene = new scene( new group() ); stage.setwidth( 450 ); stage.setheight( 550 ); table.seteditable( true ); callback<tablecolumn, tablecell> cellfactory = new callback<tablecolumn, tablecell>() { @override public tablecell call( tablecolumn p ) { return new comboboxcell(); } }; tablecolumn column1col = new tablecolumn( "column 1" ); column1col.setminwidth( 100 ); column1col.setcellvaluefactory( new propertyvaluefactory<tabvo, string>( "column1" ) ); column1col.setcellfactory( cellfactory ); column1col.setoneditcommit( new eventhandler<tablecolumn.celleditevent<tabvo, string>>() { @override public void handle( tablecolumn.celleditevent<tabvo, string> t ) { (( tabvo ) t.gettableview().getitems().get( t.gettableposition().getrow() )).setcolumn1( t.getnewvalue() ); } } ); tablecolumn column2col = new tablecolumn( "column 2" ); column2col.setminwidth( 100 ); column2col.setcellvaluefactory( new propertyvaluefactory<tabvo, string>( "column2" ) ); column2col.setcellfactory( textfieldtablecell.fortablecolumn() ); column2col.setoneditcommit( new eventhandler<tablecolumn.celleditevent<tabvo, string>>() { @override public void handle( tablecolumn.celleditevent<tabvo, string> t ) { (( tabvo ) t.gettableview().getitems().get( t.gettableposition().getrow() )).setcolumn2( t.getnewvalue() ); } } ); table.setitems( data ); table.getcolumns().addall( column1col, column2col ); (( group ) scene.getroot()).getchildren().add( table ); stage.setscene( scene ); stage.show(); } class comboboxcell extends tablecell<tabvo, string> { private combobox<string> combobox; public comboboxcell() { combobox = new combobox<>(); } @override public void startedit() { if ( !isempty() ) { super.startedit(); combobox.setitems( gettableview().getitems().get( getindex() ).getcolumn1list() ); combobox.getselectionmodel().select( getitem() ); combobox.focusedproperty().addlistener( new changelistener<boolean>() { @override public void changed( observablevalue<? extends boolean> observable, boolean oldvalue, boolean newvalue ) { if ( !newvalue ) { commitedit( combobox.getselectionmodel().getselecteditem() ); } } } ); settext( null ); setgraphic( combobox ); } } @override public void canceledit() { super.canceledit(); settext( ( string ) getitem() ); setgraphic( null ); } @override public void updateitem( string item, boolean empty ) { super.updateitem( item, empty ); if ( empty ) { settext( null ); setgraphic( null ); } else { if ( isediting() ) { settext( null ); setgraphic( combobox ); } else { settext( getitem() ); setgraphic( null ); } } } } public static void main( string[] args ) { launch( args ); } public static class tabvo { private string column1; private string column2; private observablelist<string> column1list; private tabvo( string column1, string column2, observablelist<string> column1list ) { this.column1 = column1; this.column2 = column2; this.column1list = column1list; } public string getcolumn1() { return column1; } public void setcolumn1( string column1 ) { this.column1 = column1; } public string getcolumn2() { return column2; } public void setcolumn2( string column2 ) { this.column2 = column2; } public observablelist<string> getcolumn1list() { return column1list; } public void setcolumn1list( observablelist<string> column1list ) { this.column1list = column1list; } } }
first column uses combobox in edit mode, while second column uses default factory textfields.
Comments
Post a Comment