2013-02-20 149 views

回答

2

Java7的一種可能的解決方案。爲您的手機使用cell factory。從工廠返回一個TextField。將TextField設置爲not editable。使用TextField的方法select the text你想突出顯示(可能需要一個Platform.runLater方法來做到這一點,該字段可能需要集中,以允許選擇文本)。一旦選擇完成(並且真正顯示),TextField上的Disable mouse inputfocus traversion,以便用戶無法更改選擇,也可能在Platform.runLater中。

對於Java8,您可以做類似的事情,但不是使用TextField,而是使用TextFlow並使用css或Java API在文本流中對子文本進行樣式設置。

如果您只是想突出顯示單元格中的所有文本,而不是其中的一部分,那麼您可以使用標準Label加上css樣式。部分文本突出顯示的另一個解決方案是使用帶有多個標籤的FlowPane,每個標籤都有不同的樣式。

+0

我喜歡你最後的建議。 – Sebastian 2013-02-20 13:52:21

0

有很多方法可以突出顯示tableview單元格的子字符串。我用HBoxlabel,它看起來不錯。

這裏是我的代碼:

public Callback<TableColumn<Address, String>, TableCell<Address, String>> getCellFactory() { 
     return new Callback<TableColumn<Address, String>, TableCell<Address, String>>() { 
      @Override 
      public TableCell<Address, String> call(final TableColumn<Address, String> param) { 
       final TableCell<Address, String> cell = new TableCell<Address, String>() { 
        @Override 
        protected void updateItem(final String value, final boolean empty) { 
         setGraphic(null); 
         if(null != getTableRow() && !empty) { 
          final HBox hbox = new HBox(); 
          hbox.setAlignment(Pos.CENTER_LEFT); 
          hbox.getChildren().clear(); 
          if(null == value) { 
           return; 
          } 

         final int index = value.toLowerCase().indexOf(str); 

         if(-1 == index) { 
          //no match 
          hbox.getChildren().add(new Label(value)); 
         } else { 
          //part of string before match 
          hbox.getChildren().add(new Label(value.substring(0, index))); 
          //matched part 
          final Label label = new Label(value.substring(index, index + str.length())); 
          label.setStyle("-fx-background-color: #FFFFBF;-fx-text-fill: #FF0000;"); 
          hbox.getChildren().add(label); 
          //part of string after match 
          final int lastPartIndex = index + str.length(); 
          if(lastPartIndex < value.length()) { 
           hbox.getChildren().add(new Label(value.substring(lastPartIndex))); 
          } 
         } 
         setGraphic(hbox); 
        } 
       } 
      }; 
      return cell; 
     } 
    }; 
} 
相關問題