0
A
回答
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 ;
}
相關問題
- 1. javafx-2,可編輯ComboBox值只在輸入鍵上設置
- 2. 設置其他類的值,在其他類中獲取值
- 3. 如何在comboBox中設置默認值javafx?
- 4. 設置監聽器Combobox JavaFX
- 5. 如果..其他值爲空,則將一些值設置爲NULL
- 6. 使用顯示值設置Combobox值
- 7. 如何在ComboBox javafx中設置ButtonCell?
- 8. 灰燼綁定或可觀察空置其他值設置
- 9. 變量的javafx設置值
- 10. Combobox:爲什麼comboBox_SelectionChanged在爲SelectedValue設置默認值時爲空?
- 11. Ext Designer,Combobox,在JSON商店中設置空白值
- 12. ComboBox的SelectedItem在實際值之前意外地設置爲空
- 13. RE:將值設置爲老化,但在其他鍵上出現空錯誤
- 14. JavaFX Tableview - 列值取決於其他列
- 15. mootools datepicker從其他datepicker設置的值
- 16. 其他選項 - 如何設置值?
- 17. 如何在其他表單上設置字典值?
- 18. 在空text_field上設置默認值
- 19. VBA - 變量獲取ComboBox值其他獲取TextBox值錯誤
- 20. 在ComboBox上設置SelectedIndex
- 21. iMacros簡單如果其他如果標籤有空值設置爲默認值
- 22. Ext Js Combobox - 同步呼叫設置值
- 23. 手動設置值Combobox的成員
- 24. 基於ComboBox值設置字段Java
- 25. 設置ttk的默認值Combobox
- 26. 如何設置XAML Combobox的選定值?
- 27. 如何設置comboBox的默認值?
- 28. 將DataRow設置爲ComboBox的值成員
- 29. 遠程負載ComboBox的設置值
- 30. 無法設置Combobox SelectedItem,值爲