java - javafx tableview get selected data from ObservableList -


i working on javafx project , need . while trying selected data table can selected data normal cell can't data observablelist inside tableview.

code database:

-- phpmyadmin sql dump -- version 4.0.4 -- http://www.phpmyadmin.net -- -- host: localhost -- generation time: jun 10, 2014 @ 06:20 -- server version: 5.1.33-community -- php version: 5.4.12  set sql_mode = "no_auto_value_on_zero"; set time_zone = "+00:00";   /*!40101 set @old_character_set_client=@@character_set_client */; /*!40101 set @old_character_set_results=@@character_set_results */; /*!40101 set @old_collation_connection=@@collation_connection */; /*!40101 set names utf8 */;  -- -- database: `test` --  -- --------------------------------------------------------  -- -- table structure table `customer` --  create table if not exists `customer` (   `col0` int(11) not null,   `col1` varchar(255) default null,   `col2` int(11) default null,   primary key (`col0`) ) engine=innodb default charset=latin1;  -- -- dumping data table `customer` --  insert `customer` (`col0`, `col1`, `col2`) values (12, 'adasdasd', 231), (22, 'adasdasd', 231), (212, 'adasdasd', 231);  /*!40101 set character_set_client=@old_character_set_client */; /*!40101 set character_set_results=@old_character_set_results */; /*!40101 set collation_connection=@old_collation_connection */; 

my javafx codes:

import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.util.map; import javafx.application.application; 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.button; 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.choiceboxtablecell; import javafx.scene.control.cell.textfieldtablecell; import javafx.scene.layout.borderpane; import javafx.stage.stage; import javafx.util.callback; import javafx.util.stringconverter;    class dbconnector {      private static connection conn;     private static string url = "jdbc:mysql://localhost/test";     private static string user = "root";     private static string pass = "root";      public static connection connect() throws sqlexception{         try{             class.forname("com.mysql.jdbc.driver").newinstance();         }catch(classnotfoundexception cnfe){             system.err.println("error: "+cnfe.getmessage());         }catch(instantiationexception ie){             system.err.println("error: "+ie.getmessage());         }catch(illegalaccessexception iae){             system.err.println("error: "+iae.getmessage());         }          conn = drivermanager.getconnection(url,user,pass);         return conn;     }     public static connection getconnection() throws sqlexception, classnotfoundexception{         if(conn !=null && !conn.isclosed())             return conn;         connect();         return conn;      } } public class dynamictable extends application{  object newvalue;     //table view , data     private observablelist<observablelist> data;     private tableview<observablelist> tableview;      //main executor     public static void main(string[] args) {         launch(args);     }      //connection database     public void builddata(){                         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;                             }                                                             });                     }         };           connection c ;           data = fxcollections.observablearraylist();           try{             c = dbconnector.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));                 if(j==1){                final observablelist<string> loglevellist = fxcollections.observablearraylist("fatal", "error", "warn", "info", "inout", "debug");        col.setcellfactory(choiceboxtablecell.fortablecolumn(loglevellist));             tableview.getcolumns().addall(col);            }           else{            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);             }          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                button showdatabutton = new button("add");         showdatabutton.setonaction(new eventhandler<actionevent>() {                   public void handle(actionevent event) {                       observablelist<string> row = fxcollections.observablearraylist();                  for(int i=1 ; i<=3; i++){                     //iterate column                     row.add("asdasd");                 }                  data.add(row);                //finally added tableview             tableview.setitems(data);                  }         });           tableview = new tableview();         builddata();          //main scene          borderpane root = new borderpane();         root.setcenter(tableview);         root.setbottom(showdatabutton);         scene scene = new scene(root,500,500);                  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);        }             }         });       }            } 

thank you


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -