JavaFX将行打印到TextFields中

时间:2016-08-31 00:00:30

标签: java sqlite javafx tableview

我的SQL服务器上有一个包含16行的表,我试图将BasePT列打印成一堆TextFields,但我无法弄清楚如何。我想为每一行创建一个单独的字符串?如何最小化代码并能够让每一行显示在每个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();
    }
}

3 个答案:

答案 0 :(得分:2)

如果你不想像yelliver建议的那样使用Tableview,你可以试试这个:

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添加到您想要拥有所有textFields的位置。

答案 1 :(得分: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的{​​{1}},并根据TextField文字属性的更改修改属于该列的属性:

TextField

替代

如果您不想使用@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(); } TableView将是合适的GridPane来生成这样的布局:

Pane

答案 2 :(得分: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);
        }

这会让你朝着正确的方向前进。希望它有助于/.