【问题标题】:ListView cellfactory: wrong font weightListView cellfactory:错误的字体粗细
【发布时间】:2014-06-05 06:43:30
【问题描述】:

我有一个 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);

    }
}

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

这是问题的图片:

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

【问题讨论】:

    标签: java listview javafx javafx-2 javafx-8


    【解决方案1】:

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

    看看我在下面发布的代码。如果您注释掉标有 (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);
    
          }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多