将List <simplestringproperty>分配给多个TableColumns

时间:2017-10-20 18:17:37

标签: javafx tableview

我想要一个如下所示的tableView:

id | b | a | choiceBox
--------------------------
0 | 0 | 0 | 0/1
1 | 0 | 1 | 0/1
2 | 1 | 0 | 0/1
3 | 1 | 1 | 0/1

我根据Choice Box值获取ID到我的表中。但我不知道如何将我的List位分配给多个列(即a,b,c ....

我尝试使用此处给出的示例:How can I associate data (List<SimpleStringProperty> with the table columns of a table view 但是没有用。

另外,如何将选择框添加到最后一列。

MWE: 识别TestClass:

package test;

import java.util.ArrayList;
import javafx.application.Application;
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.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewTest extends Application {

    private ObservableList<TableWrapper> items = FXCollections.observableArrayList();
    private int count;
    private TableView tv;

    @Override
    public void start(Stage primaryStage) {
        ChoiceBox cb = new ChoiceBox();
        cb.setItems(FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"));
        cb.getSelectionModel().select(0);
        cb.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue ov, Number value, Number new_value) {
                count = new_value.intValue();
                createTableData();
                createTableView();
            }
        });

        VBox root = new VBox();
        root.getChildren().add(cb);
        tv = new TableView();
        createTableData();
        createTableView();
        root.getChildren().add(tv);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Nested Obs list test");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void createTableView() {
        tv.setEditable(false);
        tv.getColumns().clear();
        tv.setPlaceholder(new Label("No truthtable"));
        tv.getColumns().clear();
        ArrayList<TableColumn> columns = new ArrayList<>();
        columns.clear();
        TableColumn<String, String> tc;
        if (count > 0) {
            tc = new TableColumn<>("#");
            tc.setResizable(false);
            tc.setSortable(false);
            tc.setCellValueFactory(
                    new PropertyValueFactory<>("id"));
            double tmp = 1 / (count + 2);
            tc.widthProperty().multiply(tmp);
            tc.setStyle("-fx-alignment: CENTER-RIGHT;");
            columns.add(tc);

        }
        for (int i = count; i > 0; i--) {
            tc = new TableColumn(Character.toString((char) (i + 96)));
            tc.setResizable(false);
            tc.setSortable(false);
            int index = i;
            /*
            tc.setCellValueFactory(cellData
                    -> new SimpleStringProperty(cellData.getValue().getBits())[index]));
             */
            double tmp = 1 / (count + 2);
            tc.widthProperty().multiply(tmp);
            columns.add(tc);
        }
        tv.getColumns().addAll(columns);
        tv.setItems(items);

    }

    private void createTableData() {
        ArrayList<TableWrapper> tmpData = new ArrayList<>();
        int max = (int) Math.pow(2, count);
        for (int i = 0; i < max; i++) {
            tmpData.add(new TableWrapper(i, count));
        }
        items = FXCollections.observableArrayList(tmpData);
    }
}

打包机:

package test;

import java.util.List;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;

public class TableWrapper {

    private final SimpleStringProperty id;
    private final List<SimpleStringProperty> bits;
    private final ChoiceBox cb;

    public TableWrapper(int id, int length) {
        this.id = new SimpleStringProperty(String.valueOf(id));
        this.cb = new ChoiceBox();
        this.bits = FXCollections.observableArrayList();
        String str = Integer.toBinaryString(id);
        for (int i = 0; i < length - str.length(); i++) {
            bits.add(new SimpleStringProperty("0"));
        }
        for (int i = 0; i < str.length(); i++) {
            bits.add(new SimpleStringProperty(String.valueOf(str.charAt(i))));
        }
        cb.setItems(FXCollections.observableArrayList("0", "1"));
        cb.getSelectionModel().select(0);
    }

    public StringProperty idProperty() {
        return id;
    }

    public String[] getBits() {
        String[] tmp = new String[bits.size()];
        for (int i = 0; i < bits.size(); i++) {
            tmp[i] = bits.get(i).toString();
        }
        return tmp;
    }
}

0 个答案:

没有答案