java - ListView细胞工厂: wrong font weight

标签 java listview javafx javafx-2 javafx-8

我有一个 ListView cellfactory,它应该用粗体字符串“GHI”绘制所有项目。问题是,甚至其他(随机)单元格也被涂成粗体。

我的代码:

static class CellFactory extends ListCell<String> {

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (!empty) {

            super.setTextFill(Color.BLACK); // set TextColor Black

            if (item != null && item.equals("GHI")) {
                super.setStyle("-fx-font-weight: bold");    //(1)
            }

        }       

        super.setText(item);

    }
}

使用调试器,仅当项目具有文本“GHI”时才执行第 (1) 行。

这是问题的图片:

different lines in bold, only GHI should be bold

我使用了 Java 1.7.0_55 32Bit 和 JavaFX 2.2.55-b13 以及 Java 1.8.0_05 32Bit 和 JavaFX 8.0.5-b13。操作系统:Win7

最佳答案

问题是,当您在其中找到字符串时,您会更改单元格的样式。但如果不是,您就不会将单元格的样式更改回正常字体。

看看我下面发布的代码。如果您注释掉标有 (2) 的行,您将看到该单元格始终保持肥胖状态。

package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());


            ListView<String> lv = new ListView<>();
            lv.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override 
            public ListCell<String> call(ListView<String> list) {
            return new CellFactory();
              }
            });

            ObservableList<String> items = FXCollections.observableArrayList();
            items.add("asd");
            items.add("fhg");
            items.add("GHI");
            items.add("tead");
            items.add("hoid");

            lv.setItems(items);

            root.setCenter(lv);

            Button btnAdd = new Button("add item");
            btnAdd.setOnMouseClicked(new EventHandler<Event>() {

                @Override
                public void handle(Event event) {
                    items.add(1, "test");
                }
            });

            root.setLeft(btnAdd);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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


    static class CellFactory extends ListCell<String> {

      @Override
      public void updateItem(String item, boolean empty) {
          super.updateItem(item, empty);

          if (!empty) {

              super.setTextFill(Color.BLACK); // set TextColor Black

              if (item != null && item.equals("GHI")) {
                  super.setStyle("-fx-font-weight: bold");    //(1)
              }
          }     
          else{
                super.setStyle("-fx-font-weight: normal");    //(2)
          }


          super.setText(item);

      }

    }
}

关于java - ListView细胞工厂: wrong font weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053332/

相关文章:

java - 使用 wsdl2java 解析 wsdl 文件会导致错误消息

java - 将来自已知时区的时间戳转换为系统时区

android - 获取一个ListView刷新? YouTube API-自定义适配器

c - UWP - 刷新 ListView 和 CollectionViewSource

java - 来自 Glassfish 的 HttpClient+SSL

java - 是否可以连接 mysql 和 sql server 以获取其中的表?

java - 将数据从 map 添加到 Java FX ListView?

java - 随线程更新场景

java - openjfx 1.8 不包含 javafx.fxml 包

JavaFX 从另一个对话框打开一个对话框