簡體   English   中英

CheckBoxTableCell無法使用ObjectProperty <Boolean>

[英]CheckBoxTableCell not working with ObjectProperty<Boolean>

我擴展了SimpleObjectProperty<T>來創建一個自定義的延遲加載實現( 參見這里 ), LazyLoadingObjectProperty<T>

要將此泛型實現用於布爾屬性,我使用LazyLoadingObjectProperty<Boolean>

在我的表中,我想將布爾屬性渲染為CheckBox

盡管如此, CheckBoxTableCell似乎只適用於BooleanProperty但不適用於ObjectProperty<Boolean>

為什么這樣,我該如何解決?

這是一些代碼:

public class ExampleTable extends Application {

    private static final int NUM_ELEMENTS = 5000;

    private final TableView<ExampleBean> table = new TableView<>();

    private final ObservableList<ExampleBean> data = FXCollections.observableArrayList();

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

    @Override
    public void start(final Stage stage) {
        final Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(500);

        final TableColumn<ExampleBean, Boolean> c1 = new TableColumn<>("A");
        c1.setCellValueFactory(new PropertyValueFactory<ExampleBean, Boolean>("p1"));
        c1.setCellFactory(CheckBoxTableCell.forTableColumn(c1));
        c1.setEditable(true);
        c1.setPrefWidth(100);

        final TableColumn<ExampleBean, String> c2 = new TableColumn<>("B");
        c2.setCellValueFactory(new PropertyValueFactory<ExampleBean, String>("p2"));
        c2.setCellFactory(TextFieldTableCell.forTableColumn());
        c2.setEditable(true);
        c2.setPrefWidth(100);


        for (int i = 0; i < NUM_ELEMENTS; i++) {
            data.add(new ExampleBean());
        }

        final ScrollPane sp = new ScrollPane();
        sp.setContent(table);
        sp.setMaxHeight(Double.POSITIVE_INFINITY);
        sp.setMaxWidth(Double.POSITIVE_INFINITY);
        sp.setFitToHeight(true);
        sp.setFitToWidth(true);

        table.setEditable(true);
        table.setItems(data);
        // table.setMaxHeight(Double.POSITIVE_INFINITY);
        // table.setMaxWidth(Double.POSITIVE_INFINITY);
        table.getColumns().addAll(c1, c2);

        final ContextMenu cm = new ContextMenu();
        cm.getItems().add(new MenuItem("bu"));
        table.setContextMenu(cm);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        VBox.setVgrow(sp, Priority.ALWAYS);
        vbox.getChildren().addAll(sp);

        scene.setRoot(vbox);

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

public class ExampleBean {

    private ObjectProperty<Boolean> p1;

    private ObjectProperty<String> p2;


    public ExampleBean() {
        p1 = new SimpleObjectProperty(true);
        p1.addListener((o, ov, nv) -> {
            System.err.println("Value changed " + ov + " -> " + nv);
        });

        p2 = new SimpleObjectProperty(Integer.toString(new Random().nextInt()));
        p2.addListener((o, ov, nv) -> {
            System.err.println("Value changed " + ov + " -> " + nv);
        });
    }

    public final ObjectProperty<Boolean> p1Property() {
        return this.p1;
    }

    public final ObjectProperty<String> p2Property() {
        return this.p2;
    }
}

請注意,使用Boolean.TRUE初始化該屬性,並且不檢查CheckBoxCells。

此問題未使用/需要:

public abstract class LazyLoadingObjectProperty<T> extends SimpleObjectProperty<T> {

    public LazyLoadingObjectProperty() {
        super();

    }

    public LazyLoadingObjectProperty(final Object bean, final String name, final T initialValue) {
        super(bean, name, initialValue);

    }

    public LazyLoadingObjectProperty(final Object bean, final String name) {
        super(bean, name);

    }

    public LazyLoadingObjectProperty(final T initialValue) {
        super(initialValue);

    }

    private boolean loaded = false;

    private final ChangeListener<T> valueChangeListener = (o, ov, nv) -> {
        valueExternallyUpdated(nv);
    };

    /**
     * Is called after the background task's finished (success or failure). Override
     * if needed. E.g. to bind the value afterwards.
     */
    protected void afterLoaded() {
        addListener(valueChangeListener);

    }

    /**
     * Returns a {@link Task} that will calculate this Property's value in the
     * background.
     *
     * @return a {@link Task} that will calculate this Property's value in the
     *         background.
     */
    protected abstract Task<T> createTask();

    protected T getFailedValue(final Throwable t) {
        return null;
    }

    @Override
    public T getValue() {
        if (!loaded) {
            startLoadingService();
        }
        return super.getValue();
    }

    public boolean isLoaded() {
        return loaded;
    }

    public void setLoaded(final boolean loaded) {



        // the loaded property has been reset manually. Remove change listener
        if (this.loaded && !loaded) {
            removeListener(valueChangeListener);
        }

        this.loaded = loaded;
    }

    @Override
    public void setValue(final T v) {
        super.setValue(v);
    }

    /**
     * Starts the {@link Task} that will calculate this Property's value in the
     * background.
     */
    protected void startLoadingService() {
        setLoaded(true);
        final Task<T> s = LazyLoadingObjectProperty.this.createTask();

        LazyLoadingThreads.getExecutorService().submit(s);

        s.setOnFailed(e -> {
            setValue(getFailedValue(e.getSource().getException()));
            afterLoaded();
        });

        s.setOnSucceeded(e -> {
            setValue(s.getValue());
            afterLoaded();
        });

    }

    /**
     * Override this callback method to handle external value-change-events
     * (not-lazily-loaded). This callback is only called, if the value is updated
     * manually, i.e., not via the lazy-loading mechanism.
     *
     * @param nv
     *            the new value
     */
    protected void valueExternallyUpdated(final T nv) {
        // override if needed

    }

}

這里找到mwe。

絕對的錯誤,至少在文檔而且在實現:自動比迪結合選中該復選框”和在索引observableValue之間-如果可能的話-是設計意圖。 即使可能,實施也無法滿足該要求。

快速入侵是修復錯誤的自定義子類,類似於(未經過正式測試,請注意!):

public static class FixedCheckBoxTableCell<S, T> extends CheckBoxTableCell<S, T> {

    @Override
    public void updateItem(T item, boolean empty) {
        checkCallback();
        super.updateItem(item, empty);
    }

    private void checkCallback() {
        if (getSelectedStateCallback() != null) return;
        ObservableValue<Boolean> observable = 
                (ObservableValue<Boolean>) getTableColumn().getCellObservableValue(getIndex());
        // handled by super
        if (observable instanceof BooleanProperty) return;
        // can't bidi-bind anyway
        if (!(observable instanceof Property)) return;
        // getting here if we have a ObjectProperty<Boolean>, that's not handled by super
        setSelectedStateCallback(index -> {
            ObjectProperty<Boolean> p = (ObjectProperty<Boolean>) getTableColumn().getCellObservableValue(index);
            return BooleanProperty.booleanProperty(p);
        });
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM