[英]JavaFX 2 ComboBox setValue() does not set CB text
我的問題是,使用setValue()選擇它后,所選的ComboBox項目文本在屏幕上不可見。 以下是一些詳細信息:將項目添加到我的CB:
combo.getItems().add("a");
combo.getItems().add("b");
combo.getItems().add("c");
combo.getItems().add("d");
之后,當按下按鈕A時:
combo.setValue(null);
按下按鈕B時:
combo.setValue("a");
現在,如果我先按按鈕B,則顯示“ a”,那就可以了。 之后,如果我按下按鈕A,則ComboBox上沒有顯示任何文本,那就可以了。 然后,我按B鍵,並且該值在屏幕上沒有更改。 但是,如果單擊CB,則“ a”的行將突出顯示,並且combo.getValue()返回“ a”。
有什么建議如何處理嗎?
我也有同樣的問題。 看起來像個錯誤。 這是一個包含ComboBox
的完整工作示例,其中包含Locale
:
package org.example;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public final class ComboBoxTest extends Application {
@Override
public void start(final Stage stage) throws Exception {
// Initialize UI
stage.setTitle("ComboBox Test");
final HBox root = new HBox(5.0f);
final ComboBox<Locale> cbLocales = new ComboBox<>();
cbLocales.setConverter(new StringConverter<Locale>() {
@Override
public String toString(final Locale locale) {
return locale.getDisplayName();
}
@Override
public Locale fromString(String string) {
throw new UnsupportedOperationException();
}
});
cbLocales.setPrefWidth(250);
HBox.setMargin(cbLocales, new Insets(10));
root.getChildren().add(cbLocales);
final Button btnFill = new Button("Fill");
HBox.setMargin(btnFill, new Insets(10));
root.getChildren().add(btnFill);
final Scene scene = new Scene(root);
stage.setScene(scene);
btnFill.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
// Fill with content
final List<Locale> locales = Arrays.asList(Locale.ENGLISH,
Locale.GERMAN, Locale.FRENCH);
final Locale defaultLocale = locales.get(1);
// cbLocales.getItems.setAll(locales) doesn't work
cbLocales.getItems().clear();
cbLocales.getItems().addAll(locales);
// Set default locale
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(
cbLocales.getValue()));
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
當ComboBox
填充的第一次,一切工作正常:該ComboBox
包含全部的三個Locale
S和第二Locale
是集。
填充第二時間之后, ComboxBox.setValue
不起作用:該ComboBox
包含全部的三個Locale
秒,但第二Locale
未設置。 未選擇任何項目,不顯示提示。
我解決了提示問題
// Set default locale
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(
cbLocales.getValue()));
但它不會選擇列表中的項目:
解決方法是:
cbLocales.getSelectionModel().select(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(cbLocales.getValue()));
選擇項目並設置提示。 但我不知道這是否存在athor問題(工具提示或類似內容)
創建組合框時,必須實例化ComboBox類並將這些項定義為可觀察列表,就像其他UI控件(如ChoiceBox,ListView和TableView)一樣。
樣例代碼:
ObservableList<String> options =
FXCollections.observableArrayList("A","B","C","D");
combo.setItems(options);
現在結果應該與您預期的一樣:)(在我的本地計算機上測試)
參考: 組合框
我認識到一種奇怪的行為。 看起來在設置“值”之前不應完成setItems()
……這對我setItems()
:
ComboBox<String> editableComboBox = new ComboBox<String>(); // <- setting the items here
// brings the "bug"
editableComboBox.setId("combobox_fields" + i);
String desciption = pair.getDescription();
editableComboBox.setValue(desciption);
editableComboBox.setEditable(true);
editableComboBox.setItems(FieldType.FIELD_TYPES); // <- here we go!
這里是值。
public static final ObservableList<String> FIELD_TYPES =
FXCollections.observableArrayList("A", "B", "C",
"D", "E", "F",
"G", "H", "I");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.