简体   繁体   English

JavaFX 2 ComboBox setValue()不会设置CB文本

[英]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(). 我的问题是,使用setValue()选择它后,所选的ComboBox项目文本在屏幕上不可见。 Here are some details: Adding items to my CB: 以下是一些详细信息:将项目添加到我的CB:

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

Afterwards, when Button A is pushed: 之后,当按下按钮A时:

combo.setValue(null);

When Button B is pushed: 按下按钮B时:

combo.setValue("a");

Now, if I push Button B first, "a" is shown, thats OK. 现在,如果我先按按钮B,则显示“ a”,那就可以了。 After that if I push Button A, no text is shown on the ComboBox, thats OK. 之后,如果我按下按钮A,则ComboBox上没有显示任何文本,那就可以了。 Then I push B, and the value did not change on the screen. 然后,我按B键,并且该值在屏幕上没有更改。 However, if I click on the CB, the row for "a" is highlighted, and combo.getValue() returns "a". 但是,如果单击CB,则“ a”的行将突出显示,并且combo.getValue()返回“ a”。

Any suggestions how to handle this? 有什么建议如何处理吗?

I have the same problem. 我也有同样的问题。 It looks like a bug. 看起来像个错误。 Here is a full working example with a ComboBox that contains Locale s: 这是一个包含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);
    }
}

When the ComboBox filled for the first time, all works fine: The ComboBox contains all 3 Locale s and the second Locale is set. ComboBox填充的第一次,一切工作正常:该ComboBox包含全部的三个Locale S和第二Locale是集。

在此处输入图片说明

After filling a second time, ComboxBox.setValue doesn't work: The ComboBox contains all 3 Locale s but the second Locale is not set. 填充第二时间之后, ComboxBox.setValue不起作用:该ComboBox包含全部的三个Locale秒,但第二Locale 设置。 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) 但我不知道这是否存在athor问题(工具提示或类似内容)

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. 创建组合框时,必须实例化ComboBox类并将这些项定义为可观察列表,就像其他UI控件(如ChoiceBox,ListView和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 参考: 组合框

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: 看起来在设置“值”之前不应完成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!

And here the values.. 这里是值。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM