更新TableView行外观 [英] Updating TableView row appearance

查看:116
本文介绍了更新TableView行外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些困难要改变一些TableView行的外观。该行应显示带有笔划和红色的文本。实际上,我可以用红色显示它,但仍然不能做中风。这是我用来改变线条外观的css类:

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 but still can't do the stroke. This is the css class I'm using to change the appearance of the line:

.itemCancelado {
    -fx-strikethrough: true;
    -fx-text-fill: red;
}

当用户将项目标记为已取消时,会添加此样式类:

This style class is added when the user mark the item as canceled:

public class ItemCanceladoCellFactory implements Callback<TableColumn, TableCell> {
    @Override
    public TableCell call(TableColumn tableColumn) {
        return new TableCell<ItemBean, Object>() {
            @Override
            public void updateItem(Object item, boolean empty) {
                super.updateItem(item, empty);
                setText(empty ? "" : getItem().toString());
                setGraphic(null);
                int indice=getIndex();
                ItemBean bean=null;
                if(indice<getTableView().getItems().size())
                    bean = getTableView().getItems().get(indice);
                if (bean != null && bean.isCancelado())
                    getStyleClass().add("itemCancelado");
            }
        };
    }
}

此处还有另一个问题,标记为已取消的行仅在用户从可观察列表中添加或删除元素时更改颜色。有没有办法可以强制更新TableView?

There is another problem here, the row marked as canceled only changes the color when the user adds or removes an element from observable list. Is there a way I can force the update of the TableView?

我更改了ItemBean类使用BooleanProperty并部分解决:

I changed the ItemBean class to use BooleanProperty and it solved partially:

public class ItemBean {
    ...
    private BooleanProperty cancelado = new SimpleBooleanProperty(false);
    ...
    public Boolean getCancelado() {
        return cancelado.get();
    }

    public void setCancelado(Boolean cancelado){
        this.cancelado.set(cancelado);
    }

    public BooleanProperty canceladoProperty(){
        return cancelado;
    }
}

不幸的是,只有列cancelado(即隐藏或删除,当这最终工作)改变外观:

Unfortunately, only the column "cancelado" (that will be hided or removed when this finally work) changes the appearance:

此处我配置列和表:

public class ControladorPainelPreVenda extends ControladorPainel {

    @FXML
    private TableView<ItemBean> tabelaItens;
    private ObservableList<ItemBean> itens = FXCollections.observableArrayList();
    ...

    private void configurarTabela() {
        colunaCodigo.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.id"));
        colunaCodigo.setCellFactory(new ItemCanceladoCellFactory());
        colunaDescricao.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.descricao"));
        colunaDescricao.setCellFactory(new ItemCanceladoCellFactory());
        colunaLinha.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.nomeLinha"));
        colunaLinha.setCellFactory(new ItemCanceladoCellFactory());
        colunaQuantidade.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("quantidade"));
        colunaQuantidade.setCellFactory(new ItemCanceladoCellFactory());
        colunaValorLiquido.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorLiquido"));
        colunaValorLiquido.setCellFactory(new ItemCanceladoCellFactory());
        colunaValorTotal.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorTotal"));
        colunaValorTotal.setCellFactory(new ItemCanceladoCellFactory());
        colunaCancelado.setCellValueFactory(new PropertyValueFactory<ItemBean, Boolean>("cancelado"));
        colunaCancelado.setCellFactory(new ItemCanceladoCellFactory());
        tabelaItens.setItems(itens);
    }
    ...
}

如何更新所有列?

推荐答案

有没有办法可以强制更新TableView?

使 Cancelado 属性为 ItemBean 类:

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

现在列表视图的默认单元格实现将侦听 cancelado 属性和触发器 updateItem 根据需要调用相关列表视图单元格。

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.

请注意,返回属性的函数的名称很重要,它必须是 canceladoProperty(),因为JavaFX扩展了标准的Java Beans成员getter和setter模式对于属性。

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.

属性命名约定背景

JavaFX属性的命名约定来自Oracle的此代码段中演示了访问权限使用JavaFX属性和绑定 tutorial。

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

}

有一个很好的概述在openfx wiki上 JavaFX Property Architecture ,详细说明了属性的命名约定和各种更高级的使用场景,如只读属性和延迟属性。

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.

自定义TableView中行的外观

此处提供了更多信息以及基于布尔行值自定义表行外观的示例 gist示例代码。

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<Friend, Boolean> makeBooleanColumn(String columnName, String propertyName, int prefWidth) {
  TableColumn<Friend, Boolean> column = new TableColumn<>(columnName);
  column.setCellValueFactory(new PropertyValueFactory<Friend, Boolean>(propertyName));
  column.setCellFactory(new Callback<TableColumn<Friend, Boolean>, TableCell<Friend, Boolean>>() {
    @Override public TableCell<Friend, Boolean> call(TableColumn<Friend, Boolean> soCalledFriendBooleanTableColumn) {
      return new TableCell<Friend, Boolean>() {
        @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);
}

相关

StackOverflow问题 JavaFX中有两种颜色的背景?,提供类似的解决方案。在该问题的答案中的讨论提供了关于JavaFX中表格突出显示的警告和细微之处的进一步信息(基本上,很难获得伪造的样式 - 聚焦环,选定的条形图,悬停反馈等 - 正确的自定义行风格)。

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).

这篇关于更新TableView行外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆