2014-11-13 4 views
3

JavaFXTableView에 표시된 텍스트의 일부를 강조하고 싶습니다. 지금까지 Text 개체를 TextFlow 개체에 사용하고 있습니다. 텍스트의 특정 부분을 강조 표시하려면 다음 코드로 강조 표시하거나 강조 표시하지 않을 부분 (javafx.scene.text 개)의 텍스트를 잘라 내기 위해 태그를 사용하고 있습니다.강조 표시된 텍스트가있는 JavaFX TableView

col3.setCellValueFactory(new PropertyValueFactory<RegexMatch, String>("text")); 
col3.setCellFactory(new Callback<TableColumn, TableCell>() { 
    @Override 
    public TableCell call(TableColumn param) { 
     TableCell cell = new TableCell() { 
      @Override 
      protected void updateItem(Object text, boolean empty) { 
       if (text != null && text instanceof String) { 
        String str = (String) text; 
        TextFlow flow = new TextFlow(); 
        if (txtSearchField.getText().length() > 3 && str.contains(HIGHLIGHT_START)) { 
         // Something to highlight 
         flow.getChildren().clear(); 
         while (str.contains(HIGHLIGHT_START)) { 
          // First part 
          Text starttext = new Text(str.substring(0, str.indexOf(HIGHLIGHT_START))); 
          starttext.setWrappingWidth(Double.MAX_VALUE); 
          flow.getChildren().add(starttext); 
          str = str.substring(str.indexOf(HIGHLIGHT_START) + HIGHLIGHT_START.length(), str.length()); 
          // Part to highlight 
          Text highlightedText = new Text(str.substring(0, str.indexOf(HIGHLIGHT_END))); 
          highlightedText.setStyle("-fx-text-background-color: yellow;"); 
          highlightedText.setFill(Color.BLUE); 
          highlightedText.setWrappingWidth(Double.MAX_VALUE); 
          flow.getChildren().add(highlightedText); 
          // Last part 
          str = str.substring(str.indexOf(HIGHLIGHT_END) + HIGHLIGHT_END.length(), str.length()); 
          if (!str.contains(HIGHLIGHT_START)) { 
           Text endtext = new Text(str); 
           endtext.setWrappingWidth(Double.MAX_VALUE); 
           flow.getChildren().add(endtext); 
          } 
         } 
        }else if (txtSearchField.getText().length() < 1) { 
         // Remove former highlightings and show simple text 
         str = str.replaceAll(HIGHLIGHT_START, ""); 
         str = str.replaceAll(HIGHLIGHT_END, ""); 
         flow.getChildren().clear(); 
         Text textModule = new Text(str); 
         textModule.setWrappingWidth(Double.MAX_VALUE); 
         flow.getChildren().add(textModule); 
        } else { 
         // show simple text 
         flow.getChildren().clear(); 
         Text textModule = new Text(str); 
         textModule.setWrappingWidth(Double.MAX_VALUE); 
         flow.getChildren().add(textModule); 
        } 
        flow.setPrefHeight(bigIcons ? BIG_SIZE : SMALL_SIZE); 
        setGraphic(flow); 
       } 
      } 
     }; 
     return cell; 
    } 
}); 

불행히도 배경 강조 표시가 작동하지 않고 그림과 같이 이상한 줄무늬가 있습니다. 텍스트에 줄 바꿈이 없습니다. (화질 죄송합니다, 자사의 실제 스크린 샷 :)

scrennshot oft table

어떤 도움에 감사드립니다

.

솔루션
는 @eckig 각 '라벨'자신의 배경 색상을 가질 수 있고 필요에 따라이 HBox에 많은 Labels 라인을 사용할 수 있기 때문에하는 HBox에 좋은 생각을 여러 Labels입니다 사용하여, 제안으로 : -fx-text-background-color를 변경하면 거기에 영향을주지 않는 이유가 그래서 this에 따르면

col3.setCellValueFactory(new PropertyValueFactory<RegexMatch, String("excerptLineTable")); 
    col3.setCellFactory(new Callback<TableColumn, TableCell>() { 
    @Override 
    public TableCell call(TableColumn param) { 
     TableCell cell = new TableCell() { 
      @Override 
      protected void updateItem(Object text, boolean empty) { 
       if (text != null && text instanceof String) { 
        HBox hbox = new HBox(); 


        String str = (String) text; 
        if (txtSearchField.getText().length() > 3 && str.contains(HIGHLIGHT_START)) { 
         // Something to highlight 
         hbox.getChildren().clear(); 
         while (str.contains(HIGHLIGHT_START)) { 
          // First part 
          Label label = new Label(str.substring(0, str.indexOf(HIGHLIGHT_START))); 
          hbox.getChildren().add(label); 
          str = str.substring(str.indexOf(HIGHLIGHT_START) + HIGHLIGHT_START.length(), str.length()); 
          // Part to highlight 
          Label label2 = new Label(str.substring(0, str.indexOf(HIGHLIGHT_END))); 
          label2.setStyle("-fx-background-color: blue;"); 
          hbox.getChildren().add(label2); 
          // Last part 
          str = str.substring(str.indexOf(HIGHLIGHT_END) + HIGHLIGHT_END.length(), str.length()); 
          if (!str.contains(HIGHLIGHT_START)) { 
           Label label3 = new Label(str); 
           hbox.getChildren().add(label3); 
          } 
         } 
        } else if (txtSearchField.getText().length() < 1) { 
         // Remove former highlightings and show simple text 
         str = str.replaceAll(HIGHLIGHT_START, ""); 
         str = str.replaceAll(HIGHLIGHT_END, ""); 
         hbox.getChildren().clear(); 
         Label label = new Label(str); 
         hbox.getChildren().add(label); 
        } else { 
         // show simple text 
         hbox.getChildren().clear(); 
         Label label = new Label(str); 
         hbox.getChildren().add(label); 
        } 
        setGraphic(hbox); 
       } 
      } 
     }; 
     return cell; 
    } 
}); 

답변

3

:

  • 사용자는이 텍스트를
  • 각 텍스트 조각을 필터링 할 수해야 TableColumn의에서 오히려 긴 텍스트있는 현재 검색어가 강조 표시 될 것과 일치합니다.
    1. 가 HBox의를 만들고 텍스트 조각을 containg 레이블을 추가

이제 당신은 두 가지 옵션이 있습니다. 레이블은 영역을 확장하므로 배경색을 가질 수 있습니다.

  • TextFlow를 사용하고 텍스트를 텍스트 조각으로 추가하십시오. 여기서 "전경색 만"변경할 수 있습니다.
  • 아 내가 잊어 버리기 전에이 : 가능한 한 많이 재활용하는 것을 시도하십시오 : 당신이 textModule.setWrappingWidth(Double.MAX_VALUE); 대신 textModule.setPrefWidth(Double.MAX_VALUE);

    그리고 또 다른 힌트를 호출하지 않아야 포장 TextFlow의 텍스트를 사용하지 않으려면. 각 업데이트에서 전체 TextFlow를 다시 만드는 것은 좋은 생각이 아닙니다 (대신 셀에 멤버 변수로 저장).

    +0

    유감스럽게도'setPrefWidth'는'Text'에서 사용할 수 없습니다. 사용자가 다른 패턴을 강조 표시하기 때문에 강조 표시된 영역이 변경되기 때문에 각각의 TextFlows를 다시 작성해야합니다. – alex

    +0

    첫 번째 : Text가 아니라 TextFlow에서 setPrefWidth를 의미했습니다. 둘째 : TextFlow를 저장하고 포함 된 Text 요소 만 다시 작성할 수 있습니다. – eckig

    +0

    1) anythink를 변경하지 않으면 텍스트가 이전처럼 랩핑됩니다. 2) 예, TextFlow 요소를 유지하기 위해 가능하지만 이후 alls Text children을 삭제하고 새로운 것들을 만들어야하기 때문에 나는이 성능을 향상시키는 것에 대해 의심 스럽습니다. ... – alex

    2

    , Text는 배경 stylable 속성이 없습니다.

    해결 방법 중 하나는이 텍스트 노드를 HBox에 배치하는 것입니다.이 노드에서 배경을 노란색으로 쉽게 설정할 수 있습니다. 그러나 이것은 텍스트 흐름이 내용을 관리하는 능력을 잃어 버리는 것과 같은 몇 가지 단점을 가지고 있습니다.

    정말로 강조 표시가 필요하면 다른 가능한 해결책은 텍스트 흐름의 배경을 스타일링하고 텍스트 범위를 찾고 적절한 인세 트를 설정하는 것입니다.

    내가 빨리 경치 Builder와 일부 CSS로 수행 한 예입니다

    .textflow { 
        -fx-background-color: yellow; 
        -fx-background-insets: 2 139.3 10 200; 
    } 
    

    Highlighted text

    내가 계정으로 TextFlow를 (510)의 폭을 찍은 및 폭의 로컬 경계를 사용하는 다른 텍스트 노드 : 200, 170.7 및 129입니다. 따라서 오른쪽 삽입은 510-200-170.7 = 139.3이고 왼쪽 삽입은 첫 번째 노드 너비 인 200에서 제공됩니다.

    이제이 문제를 해결합니다. 귀하의 방법으로 ...

    0 나는 다음과 같이 우리는이를 재개 할 수 있다고 생각을 두 번 읽는 문제 후
    +0

    HBox에서 @eckig으로 레이블을 사용하여 끝냈습니다. 감사합니다. – alex

    +0

    인세 트는 좋은 아이디어이지만 셀당 1 개의 일치로만 작동 할 수 있습니다. 맞습니까? – Joffrey