我的问题是,使用 setValue() 选择后,所选的 ComboBox 项目文本在屏幕上不可见。
以下是一些细节:
将项目添加到我的 CB:
combo.getItems().add("a");
combo.getItems().add("b");
combo.getItems().add("c");
combo.getItems().add("d");
之后,当按下按钮 A 时:
combo.setValue(null);
当按下按钮 B 时:
combo.setValue("a");
现在,如果我先按下按钮 B,就会显示“a”,那就没问题了。
之后,如果我按下按钮 A,组合框中不会显示任何文本,没关系。
然后我按B,屏幕上的值没有改变。但是,如果我单击 CB,“a”行将突出显示,并且combo.getValue() 返回“a”。
有什么建议如何处理这个问题吗?
我也有同样的问题。它看起来像一个错误。这是一个完整的工作示例ComboBox
其中包含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);
}
}
当。。。的时候ComboBox
第一次填充,一切正常:ComboBox
包含全部 3 个Locale
s 和第二个Locale
is set.
第二次填充后,ComboxBox.setValue
不起作用:ComboBox
包含全部 3 个Locale
但第二个Locale
is not放。未选择任何项目,也不显示任何提示。
我修复了提示问题
// Set default locale
cbLocales.setValue(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(
cbLocales.getValue()));
但它不会选择列表中的项目:
解决方法是:
cbLocales.getSelectionModel().select(defaultLocale);
cbLocales.setPromptText(cbLocales.getConverter().toString(cbLocales.getValue()));
选择项目并设置提示。但我不知道是否存在其他问题(工具提示或类似)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)