【问题标题】:Which RadioButton will be added first on JavaFX ToggleGroup哪个 RadioButton 将首先添加到 JavaFX ToggleGroup
【发布时间】:2017-06-01 07:54:11
【问题描述】:

说明

我有两个 RadioButtons,它们都在名为 fileSearchGroupToggleGroup 上,我使用 SceneBuilder 8 制作它们,下面是一张图片:

问题

所以添加了两个RadioButtons,如上图。如果我更改它们,使名为 File Absolute Path... 的一个下降,另一个名为 File Name... 的出现,哪个将是第一个,哪个将是 ToggleGroup 上的第二个,名为 fileSearchGroup?

为什么我想知道?

因为在应用程序内部我有这个代码(下面是一些伪代码):

//Get the index of the selected toggle
if(group.getSelectedToggleIndex() == 0)
   //Search using absolute path
else 
   //Search using file name

终于

是否有上述解决方案,或者我是否必须将ID 添加到我不想这样做的RadioButtons,因为我保存数据库设置时会增加代码复杂性。

  • 为什么我不想使用ID

我正在使用 KeyValue 数据库保存设置,因此 Key 是例如 SearchFilesUsing 并且该值是一个类似 01 的数字,基于 RadioButton 内选定的 RadioButton 索引ToggleGroup ,如果我添加ID,那么它会使工作变得困难,如果我想在未来更改ID,我将不得不在这里和那里再次更改代码。

【问题讨论】:

  • 为什么不直接将单选按钮注入控制器(例如fx:id="absolutePathRadioButton")并执行if (group.getSelectedToggle() == absolutePathRadioButton) ???
  • @James_D 你好詹姆斯 :) 。我添加了一个Finally 部分关于为什么我不使用RadioButtons 的ID 的问题,我有一个Key-Value 数据库,如果我在未来的应用程序版本中更改任何ID,它将破坏数据库。我已经构建了它,如果我现在更改它,它将为正在使用它的用户刹车。也许是一个糟糕的决定。不过,我会重新考虑如何让它像你说的那样。
  • 我不建议映射一个 id。我建议只使用对对象本身的引用。
  • @James_D 好的,所以关键是Search_Files_Using,我应该为value 使用什么,所以当我从key-value 数据库中获取值以获取用户拥有的确切RadioButton 时在关闭应用程序之前选择?顺便说一句,我使用 Java Properties 将其保存到属性文件中,这是最简单的方法。
  • 使用一些字符串常量并在需要时将它们设置为控件中的用户数据。最重要的是,您必须在文件中的内容和选择的内容之间进行一些任意映射。没有理由相信 int 比其他任何事情都重要。依赖于切换组的实现细节远不如自己显式编码。

标签: javafx radio-button


【解决方案1】:

您需要在文件中的内容(本质上是字符串,或其他数据类型的字符串表示)和单选按钮之间进行一些映射。没有任何理由相信 int 胜过一切。

您可以只使用一些字符串常量并将它们存储在控制器的映射中:

private final Map<RadioButton, String> radioButtonKeys = new HashMap<>();

private static final String ABSOLUTE_PATH_SEARCH = "ABSOLUTE_PATH";
private static final String FILE_NAME_SEARCH = "FILE_NAME";

@FXML
private RadioButton absolutePathRadioButton ;
@FXML
private RadioButton fileNameSearchRadioButton ;
@FXML
private ToggleGroup group ;

public void initialize() {
    radioButtonKeys.put(absolutePathRadioButton, ABSOLUTE_PATH_SEARCH);
    radioButtonKeys.put(fileNameSearchRadioButton, FILE_NAME_SEARCH);
}

现在你可以写入文件了

String searchType = radioButtonKeys.get(group.getSelectedToggle());
// write searchType as the value for Search_Files_Using

你可以回读

String searchType = /* value from file for Search_Files_Using */
for (RadioButton radioButton : radioButtonKeys.keySet()) {
    radioButton.setSelected(radioButtonKeys.get(radioButton).equals(searchType)) ;
}

作为一个轻微的变体,您可以创建一个枚举而不是使用字符串。这样做的好处是您实际上可以在枚举中编写搜索策略。

public enum SearchType {
    ABSOLUTE_PATH {
        public void search() {
            // search implementation here...
        }
    },
    FILE_NAME {
        public void search() {
            // search implementation here...
        }
    };

    public abstract void search() ;
}

(抽象方法可以有任何你需要的签名。)

然后像以前一样创建地图,但使用枚举值:

private final Map<RadioButton, SearchType> radioButtonKeys = new HashMap<>();

public void initialize() {
    radioButtonKeys.put(absolutePathRadioButton, SearchType.ABSOLUTE_PATH);
    radioButtonKeys.put(fileNameSearchRadioButton, SearchType.FILE_NAME);
}

文件读/写是

SearchType searchType = radioButtonKeys.get(group.getSelectedToggle());
// write searchType.toString() to file...

String searchType = /* value from file for Search_Files_Using */
for (RadioButton radioButton : radioButtonKeys.keySet()) {
    radioButton.setSelected(radioButtonKeys.get(radioButton) == SearchType.valueOf(searchType)) ;
}

枚举的优势在于您实际执行搜索时:

radioButtonKeys.get(group.getSelectedToggle()).search();

只是一些想法。切换组中切换按钮的顺序是切换组的实现,并且可能取决于非常随意的东西,例如它们在 FXML 文件中定义的顺序。恕我直言,依赖它是不可靠的。

【讨论】:

  • 太棒了!谢谢你的时间 。我会再读 15 遍来理解它;0 并实现它:)
猜你喜欢
  • 1970-01-01
  • 2017-02-04
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2017-04-04
  • 2021-01-06
  • 1970-01-01
相关资源
最近更新 更多