2013-07-06 5 views
3

행의 텍스트가 사용자가 지정한 텍스트와 일치하면 목록 상자에서 행을 강조 표시 할 수 있어야합니다 "경고"라는 단어가 포함 된 모든 줄을 강조 표시합니다. 나는 주위에 물었고 this을 보았습니다. Alex는 스타일 시트를 사용하는 방법을 보여주었습니다.JavaFX에서 문자열 값을 기반으로 목록보기의 색상을 프로그래밍 방식으로 정의하는 방법

위의 해결책을 구현했습니다. 하지만 이제는 사용자가 앞뒤 배경색을 여러 문자열 값 (우선 순위가 가장 높은 것으로 지정 됨)으로 선택해야하는 옵션을 사용자가 가질 수 있도록이 기능을 향상시키고 자합니다. CSS로 놀아서 하나 더 스타일을 추가했습니다. 텍스트가 발견 여부에 기반 I 제거하고 아래와 같은 효과를주기 위해 CSS 스타일 시트를 추가

highlighter.css

/** Highlighting for list-view search result cells */ 

.list-cell.search-highlight { 
-fx-background-color: tomato; 
-fx-accent: firebrick; 
} 

.list-cell:filled:hover.search-highlight { 
-fx-background-color: derive(tomato, -20%); 
} 

.list-cell.search-highlight2 { 
-fx-background-color: yellow; 
-fx-accent: firebrick; 
} 

.list-cell:filled:hover.search-highlight2 { 
-fx-background-color: derive(yellow, -20%); 
} 

자바 코드를 CSS를 SearchHighlightedTextCell를 추가/제거 할 수 있습니다. java

public class SearchHighlightedTextCell extends ListCell<String> { 

private static final String HIGHLIGHT_CLASS = "search-highlight"; 
private static final String HIGHLIGHT_CLASS2 = "search-highlight2"; 
private StringProperty searchText = new SimpleStringProperty(""); 
private StringProperty searchText2 = new SimpleStringProperty(""); 

SearchHighlightedTextCell(StringProperty searchText, StringProperty searchText2) { 
    this.searchText = searchText; 
    this.searchText2 = searchText2; 
} 

@Override 
protected void updateItem(String text, boolean empty) { 
    super.updateItem(text, empty); 

    setText(text == null ? "" : text); 

    updateStyleClass(); 
    searchText.addListener(new InvalidationListener() { 
     @Override 
     public void invalidated(Observable observable) { 
      updateStyleClass(); 
     } 
    }); 

    searchText2.addListener(new InvalidationListener() { 
     @Override 
     public void invalidated(Observable observable) { 
      updateStyleClass(); 
     } 
    }); 
} 

private void updateStyleClass() { 
    try { 
     if (!isEmptyString(searchText.get()) && !isEmptyString(getText()) && getText().toUpperCase().contains(searchText.get().toUpperCase())) {   
      getStyleClass().remove(HIGHLIGHT_CLASS2); 
      getStyleClass().add(HIGHLIGHT_CLASS); 

     } else if (!isEmptyString(searchText2.get()) && !isEmptyString(getText()) && getText().toUpperCase().contains(searchText2.get().toUpperCase())) { 
      getStyleClass().remove(HIGHLIGHT_CLASS); 
      getStyleClass().add(HIGHLIGHT_CLASS2);    

     } else { 
      getStyleClass().remove(HIGHLIGHT_CLASS2); 
      getStyleClass().remove(HIGHLIGHT_CLASS); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }    
} 

private boolean isEmptyString(String text) { 
    return text == null || text.equals(""); 
} 
(210)는

이이

the word Lorem was used for red and sem for yellow

같은 것을 생성하지만 것은 내가 아마도 스타일 시트 WRT의 전경색과 배경색의 모든 조합을 정의 할 수 있습니다. 사용자 환경 설정을 기반으로 프로그래밍 방식으로 스타일 시트를 수동으로 추가하는 방법이 있습니까? 또는 다른 방법이 있습니다. 여기 내가 아는 한

enter image description here

답변

4

은 목록보기에 cellfactory를 사용하여 그것을 할

listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
      @Override 
      public ListCell<String> call(ListView<String> stringListView) { 
       return new ListCell<String>(){ 
        @Override 
        protected void updateItem(String s, boolean b) { 
         super.updateItem(s, b); //To change body of overridden methods use File | Settings | File Templates. 
         if (b) { 
          setText(null); 
          setGraphic(null); 
         } 
         if (s.contains("warning")){ 
          setStyle(your style here); 
            setGraphic(your graphics); 
          setText(your text); 
         } 
         if (s.contains("error")){ 
          setStyle(your style here); 
          setGraphic(your graphics); 
          setText(your text); 
         } 
        } 
       }; 
      } 
     }); 
1

을 강조해야 내가 무엇을 표시하는 사용자를 위해 설계 한 화면입니다, 그 기능은 현재 자바 FX 2.2에 없습니다. 지금까지 내가 한 일은 CSS 파일을 프로그래밍 방식으로 작성하여 임시 파일에 쓰고 사용자가 새로운 환경 설정을 선택할 때 스타일 시트로 추가하는 것입니다. 물론 이것은 매우 어색한 해결 방법입니다.

또 다른 가능성은 분명히 덜 깨끗하지만, 구체적인 CSS 클래스를 전혀 사용하지 않고 Node#setStyle(String)을 사용하여 직접 요소의 스타일을 조작하는 것입니다. 당신은 easely 수

관련 문제