JavaFX 8 TableView: If the FixedCellSize property is being used, a column cannot be hidden

334 views Asked by At

When I paint the background of a column and then I fixed the size of a cell, this happens when I hidden a column: enter image description here

If I comment this line: "table.setFixedCellSize(24)" works correctly: enter image description here

But I need fix the size of a cell... ¿Does this error have any solutions?

Many thanks to all and sorry for my poor English.

You can run my example:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
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 Test extends Application {

    @Override
    public void start(final Stage stage) {

        // Create Items
        final ObservableList<Person> data = 
        FXCollections.observableArrayList(new Person("Ruben", "Martin"),
        new Person("Ruben", "Martin"), new Person("Ruben", "Martin"));

        // Create columns
        final TableColumn<Person, String> firstNameCol = 
        new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(
        new PropertyValueFactory<>("firstName"));

        final TableColumn<Person, String> lastNameCol = 
        new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(
        new PropertyValueFactory<>("lastName"));

        // Create Table
        final TableView<Person> table = new TableView<>();
        table.setFixedCellSize(24);
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol);

        // Create CheckBox
        final CheckBox checkLastName = new CheckBox();
        checkLastName.setText("Last Name");
        checkLastName.setSelected(true);
        lastNameCol.setStyle("-fx-background-color:yellow");
        lastNameCol.visibleProperty().
        bindBidirectional(checkLastName.selectedProperty());

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table, checkLastName);

        final Scene scene = new Scene(new Group());
        stage.setWidth(450);
        stage.setHeight(550);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    }

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

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;

        private Person(final String fName, final String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }

        public String getFirstName() {
            return this.firstName.get();
        }

        public void setFirstName(final String fName) {
            this.firstName.set(fName);
        }

        public String getLastName() {
            return this.lastName.get();
        }

        public void setLastName(final String fName) {
            this.lastName.set(fName);
        }
    }
}
1

There are 1 answers

0
Sunflame On

Probably this is a workaround but it works:

StringBinding sb = Bindings.when(lastNameCol.visibleProperty())
                .then("-fx-background-color:yellow")
                .otherwise("");
        lastNameCol.styleProperty().bind(sb);