本文介绍了填充的tableview与二维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是新的JavaFX,不知道如何从字符串的2维数组填充实现代码如下:
Am new to Javafx and wondering how to populate a tableview from a 2-dimensional array of String:
String[][] staffArray = (String[][]) connection.getAll("StaffServices");
ObservableList row = FXCollections.observableArrayList(staffArray);
//don't know what should go in here
staffTable.setItems(row);
倒很AP preciate的响应。
would really appreciate a response.
推荐答案
我认为JavaFX的应该有一个只需要二维数组,使表的方法,但它并不难使。诀窍是使用CellValueFactory获得每一列的右边的数组的索引,而不是得到一个bean的。这类似于code我用。
I think JavaFX should have a method that just takes 2d arrays and makes tables but it's not that hard to make. The trick is to use the CellValueFactory to get the right array index for each column instead of getting a bean. This is similar to code I use.
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSample extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
String[][] staffArray = {{"nice to ", "have", "titles"},
{"a", "b", "c"},
{"d", "e", "f"}};
ObservableList<String[]> data = FXCollections.observableArrayList();
data.addAll(Arrays.asList(staffArray));
data.remove(0);//remove titles from data
TableView<String[]> table = new TableView<>();
for (int i = 0; i < staffArray[0].length; i++) {
TableColumn tc = new TableColumn(staffArray[0][i]);
final int colNo = i;
tc.setCellValueFactory(new Callback<CellDataFeatures<String[], String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<String[], String> p) {
return new SimpleStringProperty((p.getValue()[colNo]));
}
});
tc.setPrefWidth(90);
table.getColumns().add(tc);
}
table.setItems(data);
root.getChildren().add(table);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
这篇关于填充的tableview与二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!