Updating TableView row appearance

前端 未结 3 551
情话喂你
情话喂你 2020-12-03 14:44

I\'m having some dificulties to change the appearance of some TableView rows. The line should show the text with a stroke and in red. Actually, I can show it in red color bu

3条回答
  •  有刺的猬
    2020-12-03 15:33

    Is there a way I can force the update of the TableView?

    Make the Cancelado a property of the ItemBean class:

    private BooleanProperty cancelado = new SimpleBooleanProperty(false);
    public BooleanProperty canceladoProperty() { 
      return cancelado;
    }
    

    Now the default cell implementation for the list view will listen for changes on the cancelado property and trigger updateItem calls for the relevant list view cell as appropriate.

    Note that the name of the function which returns the property is important, it must be canceladoProperty() as JavaFX extends the standard Java Beans member getter and setter pattern for properties.

    Property Naming Convention Background

    The naming convention for JavaFX property access is demonstrated in this code snippet from the Oracle Using JavaFX Properties and Binding tutorial.

    package propertydemo;
    
    import javafx.beans.property.DoubleProperty;
    import javafx.beans.property.SimpleDoubleProperty;
    
    class Bill {
    
        // Define a variable to store the property
        private DoubleProperty amountDue = new SimpleDoubleProperty();
    
        // Define a getter for the property's value
        public final double getAmountDue(){return amountDue.get();}
    
        // Define a setter for the property's value
        public final void setAmountDue(double value){amountDue.set(value);}
    
         // Define a getter for the property itself
        public DoubleProperty amountDueProperty() {return amountDue;}
    
    }
    

    There is a great overview of the JavaFX Property Architecture on the openfx wiki which details naming conventions for properties and various more advanced usage scenarios like read only properties and lazy properties.

    Customizing the Appearance of a Row in a TableView

    Futher information and an example of customizing table row appearance based on a boolean row value is provided in this gist sample code.

    private TableColumn makeBooleanColumn(String columnName, String propertyName, int prefWidth) {
      TableColumn column = new TableColumn<>(columnName);
      column.setCellValueFactory(new PropertyValueFactory(propertyName));
      column.setCellFactory(new Callback, TableCell>() {
        @Override public TableCell call(TableColumn soCalledFriendBooleanTableColumn) {
          return new TableCell() {
            @Override public void updateItem(final Boolean item, final boolean empty) {
              super.updateItem(item, empty);
    
              // clear any custom styles
              this.getStyleClass().remove("willPayCell");
              this.getStyleClass().remove("wontPayCell");
              this.getTableRow().getStyleClass().remove("willPayRow");
              this.getTableRow().getStyleClass().remove("wontPayRow");
    
              // update the item and set a custom style if necessary
              if (item != null) {
                setText(item.toString());
                this.getStyleClass().add(item ? "willPayCell" : "wontPayCell");
                this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
              }
            }
          };
        }
      });
      column.setPrefWidth(prefWidth);
    }
    

    Related

    The StackOverflow question Background with 2 colors in JavaFX?, provides a similar solution. The discussion in the answer to that question provides further information on caveats and subtleties of table row highlighting in JavaFX (basically, it's really hard to get the psuedo-class styles - focus rings, selected bars, hover feedback, etc - right with custom row styles).

提交回复
热议问题