2016-08-31 60 views
0

我在我的SQL服務器上有一個16行的表,我試圖將BasePT列打印到一堆TextField中,但我無法弄清楚。我想爲每一行創建一個單獨的字符串?我怎樣才能最小化代碼並能夠讓每一行顯示在每個TextField上?JavaFX將行打印到TextField中

//表100

Table 100

//按鈕

public void loadButton(){ 
    connection = SqlConnection.FormulaConnection(); 
    try { 
     String SQL = "Select * FROM '100';"; 
     ResultSet rs = connection.createStatement().executeQuery(SQL); 
     while (rs.next()) { 

//insert BasePT from Row Yellow into YellowText TextField 
      String Yellow = rs.getString("BasePt"); 
      YellowText.setText(Yellow); 

//insert BasePT from Row 012 Yellow into TwoYellowText TextField 
      String TwoYellow = rs.getString("BasePT"); 
      TwoYellowText.setText(TwoYellow); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

爲什麼你不使用TableView? – yelliver

+0

你需要'TextField's嗎?看起來像「文本」就足夠了。這裏的打印API是相關的還是「打印」在這種情況下是指「顯示」?你也可以詳細說明所需的結果嗎? 「每個TextField上都顯示每行」也沒有意義...... – fabian

回答

1

在下面的例子中,我使用了一個String[]數組的數組和行數據的元素。應該很容易使用數據庫中的數據。

的標準方式:TableView中

只需使用包含每個表列屬性Item類:

public class Item { 

    public Item(String baseFormula, String basePt) { 
     this.baseFormula = new SimpleStringProperty(baseFormula); 
     this.basePt = new SimpleStringProperty(basePt); 
    } 

    private final StringProperty basePt; 
    private final StringProperty baseFormula; 

    public final String getBaseFormula() { 
     return this.baseFormula.get(); 
    } 

    public final void setBaseFormula(String value) { 
     this.baseFormula.set(value); 
    } 

    public final StringProperty baseFormulaProperty() { 
     return this.baseFormula; 
    } 

    public final String getBasePt() { 
     return this.basePt.get(); 
    } 

    public final void setBasePt(String value) { 
     this.basePt.set(value); 
    } 

    public final StringProperty basePtProperty() { 
     return this.basePt; 
    } 

    @Override 
    public String toString() { 
     return "Item{" + "basePt=" + basePt.get() + ", baseFormula=" + baseFormula.get() + '}'; 
    } 

} 

,並使用TableView爲每個數據庫列一列。使用cellFactory顯示TextField S和修改的TextField的Text屬性的改變屬於該列的屬性:

@Override 
public void start(Stage primaryStage) { 
    TableView<Item> table = new TableView<>(); 

    Callback<TableColumn<Item, String>, TableCell<Item, String>> factory = column -> new TableCell<Item, String>() { 
     private final TextField textField; 

     { 
      textField = new TextField(); 
      textField.textProperty().addListener((observable, oldValue, newValue) -> { 
       // write to property 
       WritableValue<String> property = (WritableValue<String>) getTableColumn().getCellObservableValue(getIndex()); 
       property.setValue(newValue); 
      }); 
     } 

     @Override 
     protected void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 

      if (empty) { 
       setGraphic(null); 
      } else { 
       setGraphic(textField); 
       if (!Objects.equals(textField.getText(), item)) { 
        // only modify if TextField wasn't source of the change 
        // to prevent carret movement 
        textField.setText(item); 
       } 
      } 
     } 

    }; 

    TableColumn<Item, String> formulaColumn = new TableColumn<>("baseFormula"); 
    formulaColumn.setCellValueFactory(cd -> cd.getValue().baseFormulaProperty()); 
    formulaColumn.setCellFactory(factory); 

    TableColumn<Item, String> ptColumn = new TableColumn<>("basePt"); 
    ptColumn.setCellValueFactory(cd -> cd.getValue().basePtProperty()); 
    ptColumn.setCellFactory(factory); 

    table.getColumns().addAll(formulaColumn, ptColumn); 

    String[][] data = { 
     {"Hello", "World"}, 
     {"Hello2", "World2"}, 
     {"Hello3", "World3"}, 
     {"Hello4", "World4"}, 
     {"Hello5", "World5"}, 
     {"Hello6", "World6"} 
    }; 

    for (String[] d : data) { 
     table.getItems().add(new Item(d[0], d[1])); 
    } 

    Button btn = new Button("print"); 
    btn.setOnAction(evt -> System.out.println(table.getItems())); 

    Scene scene = new Scene(new VBox(table, btn)); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

替代

如果你不想使用TableViewGridPane將是合適的Pane產生這樣的佈局:

@Override 
public void start(Stage primaryStage) { 
    String[][] data = { 
     {"Hello", "World"}, 
     {"Hello2", "World2"}, 
     {"Hello3", "World3"}, 
     {"Hello4", "World4"}, 
     {"Hello5", "World5"}, 
     {"Hello6", "World6"} 
    }; 

    Insets margin = new Insets(4); 

    int nextRow = 1; 
    GridPane gridPane = new GridPane(); 

    Text heading1 = new Text("BaseFormula"); 
    Text heading2 = new Text("BasePT"); 

    GridPane.setMargin(heading1, margin); 
    GridPane.setMargin(heading2, margin); 

    gridPane.addRow(0, heading1, heading2); 
    for (String[] d : data) { 
     TextField tf = new TextField(d[0]); 
     TextField tf2 = new TextField(d[1]); 

     GridPane.setMargin(tf, margin); 
     GridPane.setMargin(tf2, margin); 

     gridPane.addRow(nextRow++, tf, tf2); 
    } 

    // add lines 

    // subtract stroke width 
    DoubleBinding height = gridPane.heightProperty().subtract(1); 
    // margin = 1/2 stroke width 
    Insets vMargin = new Insets(0.5, 0, 0.5, 0); 

    // add vertical lines 
    for (int i = 0; i < 3; i++) { 
     Line vLine = new Line(); 
     GridPane.setMargin(vLine, vMargin); 
     System.out.println(vLine.getStrokeWidth()); 
     vLine.endYProperty().bind(height); 
     gridPane.add(vLine, i, 0, 1, nextRow); 
    } 

    // procede accordingly with horizontal lines 
    DoubleBinding width = gridPane.widthProperty().subtract(1); 
    Insets hMargin = new Insets(0, 0.5, 0, 0.5); 

    for (int i = 0; i <= nextRow; i++) { 
     Line hLine = new Line(); 
     GridPane.setMargin(hLine, hMargin); 
     hLine.setStartX(1); 
     hLine.endXProperty().bind(width); 

     // Insert at the top of the cell 
     GridPane.setValignment(hLine, VPos.TOP); 
     gridPane.add(hLine, 0, i, 2, 1); 
    } 

    Scene scene = new Scene(new StackPane(new Group(gridPane)), 500, 500); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
2

如果你不想使用泰伯維作爲yelliver建議,你可以試試這個:

public VBox loadButton(){ 
    connection = SqlConnection.FormulaConnection(); 
    VBox vBox = new VBox(); 
    try { 
     String SQL = "Select * FROM '100';"; 
     ResultSet rs = connection.createStatement().executeQuery(SQL); 

     while (rs.next()) { 
      String yellow = rs.getString("BasePt"); 
      TextField textField = new TextField(yellow); 
      vBox.getChildren().add(textField); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return vBox; 
} 

然後,您可以將VBox添加到您想要所有textField的位置。

1

你可以用你ResultSet rs替換這個片段

ResultSet rs = null;//reference it to your rs 
     TableView<String[]> tv = new TableView<String[]>(); 
     final int columnCount = rs.getMetaData().getColumnCount(); 
     for(int i =1; i <= columnCount; i++){ 
      TableColumn<String[], String> tc = new TableColumn<String[], String>(); 
      tc.setText(rs.getMetaData().getColumnName(i)); 
      final int k = i-1; 
      tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures< 
        String[],String>, ObservableValue<String>>() { 
       @Override 
       public ObservableValue<String> call(CellDataFeatures<String[], 
         String> param) { 
        return new SimpleStringProperty(param.getValue()[k]); 
       } 
      }); 
      tc.setCellFactory(new Callback<TableColumn<String[],String>, 
        TableCell<String[],String>>() { 
       @Override 
       public TableCell<String[], String> call(TableColumn<String[], 
         String> param) { 
        return new TableCell<String[], String>(){ 
         @Override 
         protected void updateItem(String arg0, boolean arg1) { 
          super.updateItem(arg0, arg1); 
          if(arg1){ 
           setText(""); 
           return; 
          }else{ 
           setText(arg0); 
          } 

         } 
        }; 
       } 
      }); 
     } 
     while(rs.next()){ 
      String[] s = new String[columnCount]; 
      for(int i = 1; i <= columnCount; i++){ 
       s[i -1] = rs.getString(i); 
      } 
      tv.getItems().add(s); 
     } 

這會讓你在正確的方向。希望能幫助到你/。