2014-06-05 23 views
0

我有一個ListView cellfactory,它應該用粗體字符串「GHI」來繪製所有項目。問題在於即使其他(隨機)單元格也以粗體繪製。ListView cellfactory:錯誤的字體重量

我的代碼:

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」 時,纔會執行。

這是問題的一個畫面:

different lines in bold, only GHI should be bold

我使用的Java 1.7.0_55 32位使用JavaFX 2.2.55-B13 &的Java 1.8.0_05 32位使用JavaFX 8.0.5-B13。操作系統:Win7

回答

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

     } 

    } 
}