JavaFX 2 ComboBox setValue()不设置CB文本 [英] JavaFX 2 ComboBox setValue() does not set CB text

查看:1501
本文介绍了JavaFX 2 ComboBox setValue()不设置CB文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,所选的ComboBox项目文本在用setValue()选择后在屏幕上不可见。
以下是一些细节:
向我的CB添加项目:

  combo.getItems (一个); 
combo.getItems()。add(b);
combo.getItems()。add(c);
combo.getItems()。add(d);

之后,当按钮A被按下时:

  combo.setValue(null); 

按下按钮B时:

  combo.setValue(a); 

现在,如果我先按下Button B,会显示a
之后,如果我按下按钮A,没有文本显示在ComboBox上,那就OK了。
然后我按B,并且值没有在屏幕上更改。但是,如果我点击CB,a的行被突出显示,combo.getValue()返回a。



任何建议如何处理?

解决方案

我有同样的问题。它看起来像一个错误。这是一个包含 Locale ComboBox 的完整工作示例:

  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应用程序{
@Override
public void start(final Stage stage)throws Exception {
//初始化UI
阶段。 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){
//填写内容
final list< Locale> locales = Arrays.asList(Locale.ENGLISH,
Locale.GERMAN,Locale.FRENCH);
final Locale defaultLocale = locales.get(1);
/ / cbLocales.getItems.setAll(locales)不工作
cbLocales.getItems()。clear();
cbLocales.getItems()。addAll(locales);
//设置默认locale
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter()。toString(
cbLocales.getValue()));
}
} );

stage.show();
}

public static void main(String [] args){
launch(args);
}
}


$ b ComboBox 第一次填充,所有工作正常: ComboBox 包含所有3 Locale s和第二区域设置已设置。



ComboxBox.setValue



<不工作: ComboBox 包含所有3 Locale s,但第二个 Locale 设置。未选择任何项目,不显示任何提示。





我修复了

的提示问题

  //设置默认语言环境
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter()。toString(
cbLocales.getValue()));

但不选择列表中的项目:



>



一个解决方法是:

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

选择项目并设置提示。但我不知道,如果有运动员的问题(工具提示或类似)


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?

解决方案

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)

这篇关于JavaFX 2 ComboBox setValue()不设置CB文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆