【问题标题】:JAVAFX Table View Wrapped Text Font ColorJAVAFX 表格视图换行文本字体颜色
【发布时间】:2016-03-17 06:00:41
【问题描述】:
private TableColumn<FundedResearch, String> makeStringColumnRes(String columnName, String propertyName, int prefWidth) {
    TableColumn<FundedResearch, String> column = new TableColumn<>(columnName);
    column.setCellValueFactory(new PropertyValueFactory<FundedResearch, String>(propertyName));
    column.setCellFactory(new Callback<TableColumn<FundedResearch, String>, TableCell<FundedResearch, String>>() {
      @Override public TableCell<FundedResearch, String> call(TableColumn<FundedResearch, String> soCalledFriendStringTableColumn) {
        return new TableCell<FundedResearch, String>() {
            private Text text;
          @Override public void updateItem(String item, boolean empty) {

            super.updateItem(item, empty);
            if (item != null) {

                text=new Text(item.toString());
                text.setWrappingWidth(column.getWidth());
                text.setStyle("-fx-font-color:#FFFFFF");
                column.setStyle("-fx-font-color:#FFFFFF");
                this.setWrapText(true);
                setGraphic(text);
                setStyle("-fx-font-color:#FFFFFF");
            }
          }
        };
      }
    });
    if(prefWidth!=0){
    column.setPrefWidth(prefWidth);
    column.setMaxWidth(prefWidth);
    column.setMinWidth(prefWidth);
    }

    return column;
  }

我有这个方法被称为制作表格列。我已将字符串包装到单元格以使所有文本可见。问题是我不能让字体颜色变白。分享你的想法。谢谢你。 :) if statement 中设置的那些样式是我迄今为止尝试过的。谢谢

【问题讨论】:

    标签: css javafx javafx-8


    【解决方案1】:

    要使用的 CSS 属性不是 -fx-font-color

    如果应该是-fx-fill。您可以在此处找到此信息:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#shape(从Text 导航到第一个支持颜色的超类,在本例中为Shape)。

    还需要适当处理没有物品的情况。否则,当单元格为空时,text 仍将显示。此外,每次item 更改时都不需要重新创建Text 对象:

    column.setCellFactory(new Callback<TableColumn<FundedResearch, String>, TableCell<FundedResearch, String>>() {
        @Override
        public TableCell<FundedResearch, String> call(TableColumn<FundedResearch, String> soCalledFriendStringTableColumn) {
            return new TableCell<FundedResearch, String>() {
                private Text text = new Text();
    
                {
                    text.setStyle("-fx-fill: white;");
                    text.wrappingWidthProperty().bind(this.widthProperty());
                }
    
                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
    
                    if (empty || item == null) {
                        setGraphic(null);
                    } else {
                        text.setText(item);
                        setGraphic(text);
                    }
                }
            };
        }
    });
    

    【讨论】:

      【解决方案2】:

      这个也很好用。

      text.setFill(Color.WHITE);
      

      几小时前才收到

      【讨论】:

        猜你喜欢
        • 2012-11-21
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        相关资源
        最近更新 更多