【问题标题】:ComboBox selected value not getting translatedComboBox 选择的值没有被翻译
【发布时间】:2017-08-18 09:32:42
【问题描述】:

我的应用应该有多种语言。默认情况下是英语。问题是如果用户切换到不同的语言,除了ComboBox 选择的值之外的所有内容都将被翻译。看起来是这样的:

ComboBox 后面的代码是:

ObservableList<Currency> currencyItem= CurrencyDA.getCurrencies();
currenciesComboBox.setItems(currencyItem);
Callback<ListView<Currency>, ListCell<Currency>> currencyFactory = lv -> new ListCell<Currency>(){
    @Override
    protected void updateItem(Currency currency, boolean empty){
            super.updateItem(currency, empty);
            setText(empty ? "" : interfaceBundle.getString("currency_"+currency.getName()));
            }
        };
currenciesComboBox.setCellFactory(currencyFactory);
currenciesComboBox.setButtonCell(currencyFactory.call(null));
currenciesComboBox.getSelectionModel().selectFirst();

如何刷新选定的值?

【问题讨论】:

  • 您还应该注意您的更新项方法是错误的。您应该测试 item 是否为 != null 并且在 setText 到单元格之前是否不为空,否则将其设置为 null。请参考文档中的示例

标签: javafx combobox translation


【解决方案1】:

来自doc

由于 ComboBox 在内部使用 ListView 呈现内容,因此 ComboBox 类中存在 API 以允许设置自定义单元格工厂。有关单元工厂的更多信息,请参阅 Cell 和 ListCell 类。 需要注意的是,如果在 ComboBox 上设置了单元格工厂,则单元格将仅在单击 ComboBox 时显示的 ListView 中使用。如果您还想自定义 ComboBox 的“按钮”区域的呈现,可以在按钮单元格属性中设置自定义 ListCell 实例。一种方法是使用以下代码:

 //(note the use of setButtonCell): 

 Callback<ListView<String>, ListCell<String>> cellFactory = ...;
 ComboBox comboBox = new ComboBox();
 comboBox.setItems(items);
 comboBox.setButtonCell(cellFactory.call(null));
 comboBox.setCellFactory(cellFactory);

所以你唯一需要添加的是:

currenciesComboBox.setButtonCell(currencyFactory.call(null));

【讨论】:

  • 对不起,我的错,我更新了问题。没注意到我漏掉了几行T^T
  • 我将此行也放入了我的translate() 方法并且它正在工作)所以我认为这个答案是正确的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
相关资源
最近更新 更多