2014-07-09 6 views
2

Windows 7의 SceneBuilder 2.0을 사용하여 Java 8.0에서 JavaFx의 입력 양식을 e (fx) 클립으로 작성합니다.JavaFX ComboBox 항목의 텍스트 색상은 첫 번째 선택 후 변경됩니다.

나는 간단한 String ComboBox를 가지고 있으며, 목록과 선택한 문자열 모두에서 글꼴의 색과 크기를 변경하려고합니다. 내가 사용하는 CSS 코드는 선택한 항목의 텍스트를 변경합니다. 그러나 처음으로 목록을 삭제하면 검은 색 기본 글꼴로 표시됩니다. 두 번째로 모든 항목의 글꼴 색과 크기가 올바른 값으로 변경되었습니다.

글꼴 목록을 올바른 색상과 크기로 시작하려면 어떻게해야합니까?

ObservableList<String> types = FXCollections.observableArrayList 
    ("large", "medium", "small"); 

comboBox.setItems(types); 

현재 CSS : 여기

내 컨트롤러 클래스의 초기화 방법에서 코드를 단순화

#comboBox .list-cell 
{ 
    -fx-font-family: arial; 
    -fx-font-size: 16px; 
    -fx-text-fill: #a0522d; 
} 
+0

그냥 제안, 시도하십시오 combo.show() 및 combo.hide()에서 Platform.runLater, 응용 프로그램을 시작할 때. –

+0

좋은 생각이지만 작동하지 않았습니다. 이상하게도 comboBox.setStyle()을 사용하여 글꼴 패밀리와 크기를 미리 설정할 수 있지만 comboBox.setStyle ("-fx-text-fill : # a0522d;");과 같이 색상 사전 설정이 작동하지 않습니다. – MantaMan

답변

2

당신은 CellFactory을 만들 필요가 당신은 (I CSS를 사용할 수 없습니다 이유를 모르지만 작동시킬 수있는 유일한 방법입니다.) :

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class Mainu extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     ComboBox<String> cb = new ComboBox<String>(); 
     cb.setItems(FXCollections.observableArrayList("Foo","Bar","777","Batman")); 
     cb.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
      @Override public ListCell<String> call(ListView<String> p) { 
       return new ListCell<String>() { 
        @Override 
        protected void updateItem(String item, boolean empty) { 
         super.updateItem(item, empty); 
          if (item != null) { 
           setText(item); 
      //This won't work for the first time but will be the one 
      //used in the next calls 
           getStyleClass().add("my-list-cell"); 
           setTextFill(Color.RED); 
           //size in px 
           setFont(Font.font(16)); 
          } 
        } 
       }; 
      } 
     }); 
     cb.getSelectionModel().selectFirst(); 
     Pane root = new Pane(); 
     root.getChildren().add(cb); 
     Scene scene = new Scene(root); 
     scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
     stage.setScene(scene); 
     stage.show(); 
    } 
    public static void main(String[] args) {launch(args);} 
} 

전자 표준 API를 사용하여, 당신은 또한 CSS에 그것을 사용해야하고 CSS에 처음으로 프로그래밍 방식으로 설정해야한다고 믿습니다. 이것은 소프트웨어를 유지 관리하는 사람에게 유용하기 때문입니다.

있는 style.css

.combo-box .cell{ 
    -fx-text-fill: blue; 
    -fx-font: 16px "Arial"; 
} 
.my-list-cell { 
    -fx-text-fill: blue; 
    -fx-font: 16px "Arial"; 
    /* No alternate highlighting */ 
    -fx-background-color: #FFF; 
} 

호기심 세부하십시오 CellFactory를 사용했다 여전히 could set this via CSS 자바 FX 2 만.

관련 문제