JavaFX 2 ComboBox setValue () does not set CB text

My problem is that the selected text of the ComboBox element does not appear on the screen after selecting it using setValue (). Here are a few details: Adding items to my CB:

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

Then, when button A is pressed:

 combo.setValue(null); 

When button B is pressed:

 combo.setValue("a"); 

Now, if I press button B first, "a" is displayed, it is OK. After that, if I press button A, the text is not displayed on the ComboBox, this is normal. Then I press B and the value has not changed on the screen. However, if I click on CB, the string for "a" is highlighted, and combo.getValue () returns "a".

Any suggestions on how to handle this?

+4
source share
3 answers

I have the same problem. This seems like a mistake. The following is a complete working example with a ComboBox that contains Locale s:

 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 fills up for the first time, everything works fine: the ComboBox contains all 3 Locale , and the second Locale installed.

enter image description here

After filling the second time, ComboxBox.setValue does not work: ComboBox contains all 3 Locale , but the second Locale not installed. The item is not selected, and the prompt is not displayed.

enter image description here

I fixed the problem with

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

but it does not select an item in the list:

enter image description here

It works like this:

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

To select an item and set an invitation. But I do not know if there are problems with this (tool tips or similar)

+6
source

When creating a combo box, you must create an instance of the ComboBox class and define the items as an observable list, as well as other user interface controls such as ChoiceBox, ListView, and TableView.

Code example:

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

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

Link: Combo Box

0
source

I found out weird behavior. It seems that setItems() does not need to be done before you set your โ€œvalueโ€ ... Here is the code that 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 are the meanings ..

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

Source: https://habr.com/ru/post/1447350/


All Articles