Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回字符串; } }); } }; 如果(j!=1){ setCellFactory上校(cellFactoryForMap); } System.out.println(“列[“+i+”]); } /** * **********************_Java_Javafx - Fatal编程技术网

Java 返回字符串; } }); } }; 如果(j!=1){ setCellFactory上校(cellFactoryForMap); } System.out.println(“列[“+i+”]); } /** * **********************

Java 返回字符串; } }); } }; 如果(j!=1){ setCellFactory上校(cellFactoryForMap); } System.out.println(“列[“+i+”]); } /** * **********************,java,javafx,Java,Javafx,返回字符串; } }); } }; 如果(j!=1){ setCellFactory上校(cellFactoryForMap); } System.out.println(“列[“+i+”]); } /** * ****************************** *添加到ObservableList的数据* ******************************* */ while(rs.next()){ //迭代行 ObservableList行=FXCollections.o

返回字符串; } }); } }; 如果(j!=1){ setCellFactory上校(cellFactoryForMap); } System.out.println(“列[“+i+”]); } /** * ****************************** *添加到ObservableList的数据* ******************************* */ while(rs.next()){ //迭代行 ObservableList行=FXCollections.observableArrayList();
对于(int i=1;i什么不起作用。当您在侦听器中为所选项目设置标签时,您不是正在获取所选项目吗?不,我想从tableview从头到尾获取完整的数据。使用循环或任何东西可以获取完整的最后一行数据,因为我想将数据插入Mysql表。我无法运行您的代码,但
for(Object o:(observateList)newValue){…}
应该做你想做的事情。change listener的
newValue
就是你用来创建表的类。在你的例子中,它只是一个没有类型信息的observateList。
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;
    }
}
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 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<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<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 to 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 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<>("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 the row is not empty
    @Override
    protected void updateItem(Boolean t, boolean empty) {
        super.updateItem(t, empty);
        if(!empty){
            setGraphic(cellButton);
        }
    }
}

} 
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 you can use derby in your 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);
            for (int i = 0; i < 10; i++) {
                sql = "INSERT INTO CUSTOMER VALUES (" + i + ",'1st string in row " + i + "','2nd string in row " + i + "')";
                stmt.executeUpdate(sql);
            }
            //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 < 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<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 [" + i + "] ");
            }

            /**
             * ******************************
             * Data added to 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);
            }

            rs.close();
            c.close();
            //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();
        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 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);
                    System.out.println("Selected row " + newValue);
                }
            }
        });

        System.out.println("");
        System.out.println("List all data without using table");
        for (ObservableList<String> ol : data){
            System.out.print("row-> ");
            for (String s : ol){
                System.out.print(s+" ");
            }
            System.out.println("");
        }
        System.out.println("");
        System.out.println("Show data at 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 at 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);
    }

}