Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2 ComboBox setValue() does not set CB text

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?

like image 739
Anvar Avatar asked Nov 20 '12 22:11

Anvar


1 Answers

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.

enter image description here

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.

enter image description here

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:

enter image description here

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)

like image 161
Vertex Avatar answered Sep 19 '22 11:09

Vertex