JavaFX 2 ComboBox setValue() does not set CB text

老子叫甜甜 提交于 2020-01-14 12:38:46

问题


My problem is that the selected ComboBox item text is not visible on the screen after selecting it with setValue(). Here are some details: Adding items to my CB:

combo.getItems().add("a");
combo.getItems().add("b");
combo.getItems().add("c");
combo.getItems().add("d");

Afterwards, when Button A is pushed:

combo.setValue(null);

When Button B is pushed:

combo.setValue("a");

Now, if I push Button B first, "a" is shown, thats OK. After that if I push Button A, no text is shown on the ComboBox, thats OK. Then I push B, and the value did not change on the screen. However, if I click on the CB, the row for "a" is highlighted, and combo.getValue() returns "a".

Any suggestions how to handle this?


回答1:


I have the same problem. It looks like a bug. Here is a full working example with a ComboBox that contains Locales:

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);
    }
}

When the ComboBox filled for the first time, all works fine: The ComboBox contains all 3 Locales and the second Locale is set.

After filling a second time, ComboxBox.setValue doesn't work: The ComboBox contains all 3 Locales but the second Locale is not set. No item is selected an no prompt is shown.

I fixed the prompt problem with

// Set default locale
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(
        cbLocales.getValue()));

but it doesn't select the item in the list:

A work around is that:

cbLocales.getSelectionModel().select(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(cbLocales.getValue()));

To select the item and set the prompt. But I don't know, if there are athor problems with that (tool tips or similar)




回答2:


When creating a combo box, you must instantiate the ComboBox class and define the items as an observable list, just like other UI controls such as ChoiceBox, ListView, and TableView.

Sample Code :

ObservableList<String> options = 
    FXCollections.observableArrayList("A","B","C","D");

combo.setItems(options);

now results should be as u expected :) (tested on my local machine)

Reference : Combo Box




回答3:


I recognized a strange behavior. It looks like the setItems() should not be done before you set your "value"... Here's some code which works for me:

 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!

And here the values..

public static final ObservableList<String> FIELD_TYPES =
            FXCollections.observableArrayList("A", "B", "C",
                                              "D", "E", "F",
                                              "G", "H", "I");


来源:https://stackoverflow.com/questions/13483025/javafx-2-combobox-setvalue-does-not-set-cb-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!