title: javafx tableview how to get selected data without using class
body: <p>I am working on a javafx project and i want to get selected data from tableview but i don't want to use class because number of columns are not fixed .So, please help me.</p>
  
  <p>for example:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>This class can only be use for two column table , but in my project client can add column as per his need so please help me to get selected data from tableview</p>
  
  <p>More code here:</p>
  
  <pre><code>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&lt;ObservableList&gt; data;
  private TableView&lt;ObservableList&gt; tableview;
  
  
  public static void main(String[] args) {
      launch(args);
  }
  
  //CONNECTION DATABASE
  public void buildData(){
        Connection c ;
        data = FXCollections.observableArrayList();
        try{
          c = DBConnect.connect();
          //SQL FOR SELECTING ALL OF CUSTOMER
          String SQL = "SELECT * from CUSTOMer";
          //ResultSet
          ResultSet rs = c.createStatement().executeQuery(SQL);
  
          /**********************************
           * TABLE COLUMN ADDED DYNAMICALLY *
           **********************************/
          for(int i=0 ; i&lt;rs.getMetaData().getColumnCount(); i++){
              //We are using non property style for making dynamic table
              final int j = i;                
              TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
              col.setCellValueFactory(new Callback&lt;CellDataFeatures&lt;ObservableList,String&gt;,ObservableValue&lt;String&gt;&gt;(){                    
                  public ObservableValue&lt;String&gt; call(CellDataFeatures&lt;ObservableList, String&gt; param) {                                                                                              
                      return new SimpleStringProperty(param.getValue().get(j).toString());                        
                  }                    
              });
  
              tableview.getColumns().addAll(col); 
                   tableview.setEditable(true);
       Callback&lt;TableColumn&lt;Map, String&gt;, TableCell&lt;Map, String&gt;&gt;
          cellFactoryForMap = new Callback&lt;TableColumn&lt;Map, String&gt;,
              TableCell&lt;Map, String&gt;&gt;() {
                  @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 to ObservableList *
           ********************************/
          while(rs.next()){
              //Iterate Row
              ObservableList&lt;String&gt; row = FXCollections.observableArrayList();
              for(int i=1 ; i&lt;=rs.getMetaData().getColumnCount(); i++){
                  //Iterate Column
                  row.add(rs.getString(i));
              }
              System.out.println("Row [1] added "+row );
              data.add(row);
  
          }
  
          //FINALLY ADDED TO 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 is selected and set value of selected item to 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&lt;&gt;("Action");
      col_action.setSortable(false);
  
      col_action.setCellValueFactory(
              new Callback&lt;TableColumn.CellDataFeatures&lt;JavaFXDynTable2.Record, Boolean&gt;, 
              ObservableValue&lt;Boolean&gt;&gt;() {
  
          @Override
          public ObservableValue&lt;Boolean&gt;         call(TableColumn.CellDataFeatures&lt;JavaFXDynTable2.Record, Boolean&gt; p) {
              return new SimpleBooleanProperty(p.getValue() != null);
          }
      });
  
      col_action.setCellFactory(
              new Callback&lt;TableColumn&lt;JavaFXDynTable2.Record, Boolean&gt;,     TableCell&lt;JavaFXDynTable2.Record, Boolean&gt;&gt;() {
  
          @Override
          public TableCell&lt;JavaFXDynTable2.Record, Boolean&gt; call(TableColumn&lt;JavaFXDynTable2.Record, Boolean&gt; p) {
              return new ButtonCell(tableview);
          }
  
      });
      tableview.getColumns().add(col_action);
  
    }
     private class ButtonCell extends TableCell&lt;JavaFXDynTable2.Record, Boolean&gt; {
      final CheckBox cellButton = new CheckBox();
  
      ButtonCell(final TableView tblView){
  
          cellButton.setOnAction(new EventHandler&lt;ActionEvent&gt;(){
  
              @Override
              public void handle(ActionEvent t) {
  
  }
  
  
  
  
          });
      }
  
      //Display button if the row is not empty
      @Override
      protected void updateItem(Boolean t, boolean empty) {
          super.updateItem(t, empty);
          if(!empty){
              setGraphic(cellButton);
          }
      }
  }
  
  } 
  </code></pre>