组合框清算值问题 [英] Combobox clearing value issue

查看:143
本文介绍了组合框清算值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个javafx2.2中的Comboboxes的问题。这是场景:




  • 用户点击editFile按钮。

  • visible(使用setVisible方法)。



此窗格包含6个组合框。
其中三个有固定项目:cboReport,cboSales,cboSend。其中三个从数据库(ObservableList)获取数据并在窗格变为可见时填充:cboFile,cboCustomer,cboVet




  • 从cboFile中选择一个文件编号。

  • 用户按下保存按钮,文件会按预期保存。

  • 下一步


  • 窗口关闭时,窗格上的数据通过resetGUI_editFilePane()方法重置。有如下的行:

      ... 
    cboReport.getSelectionModel()。clearSelection
    cboSales.getSelectionModel()。clearSelection();
    cboSend.getSelectionModel()。clearSelection();
    cboFile.getSelectionModel()。clearSelection();
    cboCustomer.getSelectionModel()。clearSelection();
    cboVet.getSelectionModel()。clearSelection();

    cboFile.getItems()。clear();
    cboCustomer.getItems()。clear();
    cboVet.getItems.clear();
    ...

    当用户通过按'editFile'按钮再次打开窗格我注意到,只有'固定项目'组合框已经清除他们的选择,动态填充组合框显示最后选择的项目,虽然选择本身的值 null



    有什么办法解决这个问题,或者什么是最好的方法来重置一个组合框?



    EDIT 2014/08/27:

    这是官方不是一个错误(clearSelection()不清除值):

    https://bugs.openjdk.java.net/browse/ JDK-8097244



    官方的解决方法是在清除选择后清除ComboBox的值。

      cb.getSelectionModel()。clearSelection 
    //清除ComboBox的值,因为clearSelection()不会这样做
    cb.setValue(null);


    解决方案

    我遇到了几乎完全相同的情况,你的问题,同时寻找一个解决方案。幸运的是,我想出了一个解决方法,强制ComboBoxes重置。当你重置窗格中的数据,而不是像下面这样:

      cboVet.getSelectionModel()。clearSelection 
    cboVet.getItems.clear();

    执行这样的操作...

      parentNode.getChildren()。remove(cboVet); 
    cboVet = new ComboBox(); //做任何你需要的格式化你的ComboBox
    parentNode.add(cboVet);

    你还需要在ComboBox上再做一次setItems填充。这不是一个理想的解决方案,但它似乎工作,因为我期望提供的clearSelection()方法。


    I've stumbled on an issue with Comboboxes in javafx2.2. This is the scenario:

    • Users click on the 'editFile' button.
    • Another pane becomes visible (with the setVisible method).

    This pane contains 6 comboboxes. Three of them have fixed items: cboReport, cboSales, cboSend. Three of them get their data from a db (ObservableList) and get populated when the pane becomes visible: cboFile, cboCustomer, cboVet

    • The user selects a file number from the cboFile. The rest of the comboboxes are beeing set with the correct values.
    • The user presses the save button, the file gets saved as intended.
    • Next the user presses a close button.

    When the window closes, the data on the pane gets resetted through a resetGUI_editFilePane() method. There is have lines like:

    ...
    cboReport.getSelectionModel().clearSelection();
    cboSales.getSelectionModel().clearSelection();
    cboSend.getSelectionModel().clearSelection();
    cboFile.getSelectionModel().clearSelection();
    cboCustomer.getSelectionModel().clearSelection();
    cboVet.getSelectionModel().clearSelection();
    
    cboFile.getItems().clear();
    cboCustomer.getItems().clear();
    cboVet.getItems.clear();
    ...
    

    When the user opens the pane again by pressing the 'editFile' button I notice that only the 'fixed item' comboboxes have cleared their selection, the dynamicly filled comboboxes show the last selected item although the value from the selection itself is null. This looks like a graphics bug to me or am I doing something wrong?

    Is there any way around this issue or what is the best method to reset a combobox?

    EDIT 2014/08/27:
    This is officially not a bug(clearSelection() does not clear value):
    https://bugs.openjdk.java.net/browse/JDK-8097244

    The official "workaround" is to clear the value of the ComboBox after clearing selection.

    cb.getSelectionModel().clearSelection();
    // Clear value of ComboBox because clearSelection() does not do it
    cb.setValue(null);
    

    解决方案

    I ran into nearly the exact same situation and came across your question while looking for a solution. Fortunately, I came up with a workaround that forces the ComboBoxes to reset. When you reset the data on your pane, instead of doing something like:

    cboVet.getSelectionModel().clearSelection();
    cboVet.getItems.clear();
    

    do something like this...

    parentNode.getChildren().remove(cboVet);
    cboVet = new ComboBox();  // do whatever else you need to format your ComboBox
    parentNode.add(cboVet);
    

    You'll also need to do a setItems() again on your ComboBox so the new one will be populated. This is not an ideal solution but it seems to be working as I expect the provided clearSelection() method would.

    这篇关于组合框清算值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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