2015-06-16 31 views

回答

1

使用細胞工廠配置在下拉單元的顯示。如果你想讓一些值與其他值不同,最好的方法是在單元格上設置一個CSS僞類,並使用外部CSS文件來定義樣式。

要配置所選項目的顯示,請在組合框上設置buttonCell

下面是一個完整的例子:

import java.util.Locale; 

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.css.PseudoClass; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class LanguageComboBoxExample extends Application { 

    private static final Locale BLANK = new Locale(""); 

    @Override 
    public void start(Stage primaryStage) { 
     ComboBox<Locale> langCombo = new ComboBox<>(FXCollections.observableArrayList(
       Locale.FRENCH, 
       Locale.ENGLISH, 
       Locale.GERMAN, 
       BLANK    
     )); 

     PseudoClass otherOptionPseudoClass = PseudoClass.getPseudoClass("other-option"); 

     langCombo.setCellFactory(lv -> new ListCell<Locale>() { 
      @Override 
      public void updateItem(Locale language, boolean empty) { 
       super.updateItem(language, empty); 
       if (empty) { 
        setText(null); 
        pseudoClassStateChanged(otherOptionPseudoClass, false); 
       } else { 
        if (language == BLANK) { 
         setText("Other"); 
         pseudoClassStateChanged(otherOptionPseudoClass, true); 
        } else { 

         // this gives the display you have: 
         setText(language.getLanguage()); 

         // I prefer this for usability: 
         // setText(language.getDisplayLanguage(language)); 

         pseudoClassStateChanged(otherOptionPseudoClass, false); 
        } 
       } 
      } 
     }); 

     langCombo.setButtonCell(new ListCell<Locale>() { 
      @Override 
      public void updateItem(Locale language, boolean empty) { 
       super.updateItem(language, empty); 
       if (language == null || language == BLANK) { 
        setText(null); 
       } else { 
        setText(language.getLanguage()); 
       } 
      } 
     }); 

     StackPane root = new StackPane(langCombo); 
     Scene scene = new Scene(root, 175, 120); 
     scene.getStylesheets().add("language-combo.css"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

CSS文件(語言combo.css):

.list-cell:other-option { 
    -fx-opacity: 0.5 ; 
} 
相關問題