用二维数组填充表视图

6

我是一个新手,想知道如何使用2维字符串数组填充tableview:

    String[][] staffArray = (String[][]) connection.getAll("StaffServices");
    ObservableList row = FXCollections.observableArrayList(staffArray);

    //don't know what should go in here

    staffTable.setItems(row);

would really appreciate a response.

3个回答

16

我认为JavaFX应该有一个方法,只需要传入2D数组就可以创建表格,但这并不难实现。窍门在于使用CellValueFactory来获取每列的正确数组索引,而不是获取bean对象。这与我使用的代码类似。

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();
    }
}

谢谢,它可以工作了!需要一些微调。这让我差点秃头了。不过期待JavaFX API的到来。 - Abiodun Osinaike

4

我正在开发一个分布式(客户端/服务器)解决方案,由于序列化/反序列化的限制,该模型无法在客户端上使用。我认为这在JavaFX中是可能的,就像在Swing中一样。 - Abiodun Osinaike
找到了一种通过使用数组中的值来初始化模型的构建方法。效果不错,谢谢! - Abiodun Osinaike

-1

除了Brian的回答之外,我还添加了适当的CellFactory,以避免在查看具有良好设计的结果时使用toString()方法。我的解决方案在使用模型-视图-控制器模型时是可以的。以下是控制器块的代码:

@FXML
private TableView<String[]> table = new TableView<>();

@FXML
public void initialize() {


    //binding our ObservableList with TableView
    String[][] staffArray = {{"a", "b", "c"},
                             {"d", "e", "f"}};
    ObservableList<String[]> data = FXCollections.observableArrayList();
    data.addAll(Arrays.asList(staffArray));

    for (int i = 0; i < data.get(0).length; i++) {
        TableColumn tc = new TableColumn();
        tc.setSortable(false);


        final int colNo = i;
        tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String[], String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<String[], String> p) {
                return new SimpleStringProperty((p.getValue()[colNo]));
            }
        });
        tc.setCellFactory(col -> {
            TableCell<String[], String> cell = new TableCell<>();

            cell.itemProperty().addListener((observableValue, o, newValue) -> {
                if (newValue != null) {
                    Node graphic = createPriorityGraphic(newValue);
                    cell.graphicProperty().bind(Bindings.when(cell.emptyProperty()).then((Node) null).otherwise(graphic));
                }
            });
            return cell;
        });

        table.getColumns().add(tc);
    }


    // making Headers of table hidden
    table.widthProperty().addListener(new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
            // Get the table header
            Pane header = (Pane) table.lookup("TableHeaderRow");
            if (header != null && header.isVisible()) {
                header.setMaxHeight(0);
                header.setMinHeight(0);
                header.setPrefHeight(0);
                header.setVisible(false);
                header.setManaged(false);
            }
        }
    });

    table.setItems(data);

    table.setSelectionModel(null);

    table.setMaxSize(315.0, 502.0);

}

//prepare some special design for tableview output, if needed
@FXML
private Node createPriorityGraphic(String value) {
    if (!value.equals("0") && value != "") {
        Rectangle graphic = new Rectangle();
        graphic.setHeight(25);
        graphic.setWidth(25);
        graphic.setOpacity(80);
        return graphic;
    }
    return null;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接