复选框列表并与另一个字符串数组检查文本值

huangapple go评论60阅读模式
英文:

jCheckbox list and check text value with another string array

问题

I can provide translations for the non-code parts of your text:

在应用程序中,我可以导出文件,并且我正在尝试创建一个导入功能。我可以导入文本文件,但我的问题是以下的情况。

我有几个复选框。请看下面的图片。当我导出文件时,我创建一个包含所有选定品牌的.txt文件。

当我导入.txt文件时,我当然可以创建一个包含所有值的数组列表。我想要将该列表与所有复选框进行交叉检查并“勾选”这些框。例如,我的列表中有“avene”,我希望“avene”复选框(或ck01)被选中。但我不确定如何继续。

这是我导入文件并创建数组的方式:

BufferedReader bf1 = new BufferedReader(new FileReader(importpath + "\\additional_info\\brands.txt"));
String line1 = bf1.readLine();
selectedBrands = new ArrayList();
while(line1 != null) {
    line1 = bf.readLine();
    selectedBrands.add(line1);
}

这是我开始尝试做的。我尝试过一些方法,比如创建复选框数组,包含复选框的对象jlist。我甚至尝试创建一个新的jcheckbox列表,但我总是会收到错误消息:

.getText()命令上找不到符号。

我添加了我使用Object[]列表进行的最新测试,但我尝试过其他几种方法。

List<Checkbox> checkboxes = new ArrayList<Checkbox>();
Object[] newList = {ck01, ck02, ck03, ck04, ck05, ck06, ck07, ck08, ck09, ck10, ck11, ck12, ck13, ck14, ck15, ck16, ck17, ck18, ck19, ck20, ck21, ck22, ck23, ck24, ck25, ck26, ck27, ck28, ck29, ck30, ck31, ck32, ck33, ck34, ck35, ck36, ck37, ck38, ck39, ck40, ck41, ck42, ck43, ck44, ck45, ck46, ck47, ck48, ck49, ck50, ck51, ck52, ck53, ck54};

for (int i = 0; i < newList.length; i++) {

    newList[i].getText();
}

***附:该应用程序是使用NetBeans创建的!

英文:

In the app, I can export files and I am trying to create an import function as well. I do import the text files, but my issue is the following.

I have several checkboxes. See the image bellow. When I am exporting the file, I create a .txt that includes all the brands selected.

When I import the .txt, I can of course create an arraylist with all the values. I want to cross-check that list against all the checkboxes and "check" the boxes. For example, I have "avene" inside my list, and I want the "avene" checkbox (or ck01) to be checked. But I am unsure on how to proceed.

复选框列表并与另一个字符串数组检查文本值

This is the way I am importing the file and creating an array:

BufferedReader bf1 = new BufferedReader(new FileReader(importpath + &quot;\\additional_info\\brands.txt&quot;));
String line1 = bf1.readLine();
selectedBrands = new ArrayList();
while(line1 != null) {
    line1 = bf.readLine();
    selectedBrands.add(line1);
}

And this is that I started trying to do. I tried a few things, like creating checkbox array, an object jlist that includes the checkboxes. I even tried creating a new jcheckbox list, but I am always getting an error:

cannot find symbol on the .getText() command.

I am adding the latest test I did with the Object[] list, but I have tried several other ways.

List&lt;Checkbox&gt; checkboxes = new ArrayList&lt;Checkbox&gt;();
Object[] newList = {ck01, ck02, ck03, ck04, ck05, ck06, ck07, ck08, ck09, ck10, ck11, ck12, ck13, ck14, ck15, ck16, ck17, ck18, ck19, ck20, ck21, ck22, ck23, ck24, ck25, ck26, ck27, ck28, ck29, ck30, ck31, ck32, ck33, ck34, ck35, ck36, ck37, ck38, ck39, ck40, ck41, ck42, ck43, ck44, ck45, ck46, ck47, ck48, ck49, ck50, ck51, ck52, ck53, ck54};

for (int i = 0; i &lt; newList.length; i++) {

    newList[i].getText();
}

***P.S. the app was created using NetBeans!

答案1

得分: 1

以下是翻译好的部分:

首先,有两种类型的复选框,一种是 java.awt.Checkbox,另一种是 javax.swing.JCheckBox

如果您正在使用 JCheckBox,请考虑以下代码。

如果您正在使用 Checkbox,而不是 isSelected,您应该使用 getState,设置方法也是一样的。而且,不要使用 getText,请使用 getLabel,以及设置方法。

请记住,只有在每个复选框的文本是唯一的情况下,这才有效。

关于使用 Object 数组,如果您使用对象数组,将会提高可扩展性,因此在这种情况下可以使用 JCheckBox[]Checkbox[] 中的任何一个。

更好的做法是使用可变的 List&lt;JCheckBox&gt;

此外,值得注意的是,Java会转换您的路径分隔符,因此不必取消转义 \,只需使用单个 /

以下是代码中的一部分示例:

List&lt;String&gt; getSelected() {
    // ...
}

void saveSelected() throws IOException {
    // ...
}

void loadSelected() throws IOException {
    // ...
}

希望这有所帮助!如果您需要更多信息,请随时提问。

英文:

First, there are two types of checkboxes, there's a java.awt.Checkbox, and a javax.swing.JCheckBox.

If you're using the JCheckBox, consider the following code.

If you're using the Checkbox, instead of isSelected, you would use getState, same with the setter. And, instead of getText, you would use getLabel, as well as the setter method.

Keep in mind, this will be effective only if the text of each checkbox is unique.

In response to the use of an Object array, it will improve scalability if you use an array of the object, so in this case either a JCheckBox[] or a Checkbox[].

Even better, use a mutable List&lt;JCheckBox&gt;.

Additionally, it's worth noting that Java will convert your path separators, so instead of un-escaping the \, you can just use a single /.

List&lt;String&gt; getSelected() {
    List&lt;String&gt; selected = new ArrayList&lt;&gt;();
    JCheckBox[] newList = { ck01, ck02, ck03, ck04, ck05, ck06, ck07, ck08, ck09, ck10, ck11, ck12, ck13, ck14, ck15, ck16, ck17, ck18, ck19, ck20, ck21, ck22, ck23, ck24, ck25, ck26, ck27, ck28, ck29, ck30,ck31, ck32, ck33, ck34, ck35, ck36, ck37, ck38, ck39, ck40,ck41, ck42, ck43, ck44, ck45, ck46, ck47, ck48, ck49, ck50,ck51, ck52, ck53, ck54 };
    for (JCheckBox checkbox : newList) {
        if (checkbox.isSelected())
            selected.add(checkbox.getText());
    }
    return selected;
}

void saveSelected() throws IOException {
    String path = importpath + &quot;\\additional_info\\brands.txt&quot;;
    try (FileWriter writer = new FileWriter(path)) {
        String newline = System.lineSeparator();
        for (String checkboxText : getSelected()) {
            writer.write(checkboxText);
            writer.write(newline);
        }
        writer.flush();
    }
}

void loadSelected() throws IOException {
    String path = importpath + &quot;\\additional_info\\brands.txt&quot;;
    try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
        JCheckBox[] checkboxes = { ck01, ck02, ck03, ck04, ck05, ck06, ck07, ck08, ck09, ck10, ck11, ck12, ck13, ck14, ck15, ck16, ck17, ck18, ck19, ck20, ck21, ck22, ck23, ck24, ck25, ck26, ck27, ck28, ck29, ck30,ck31, ck32, ck33, ck34, ck35, ck36, ck37, ck38, ck39, ck40,ck41, ck42, ck43, ck44, ck45, ck46, ck47, ck48, ck49, ck50,ck51, ck52, ck53, ck54 };
        String line;
        while ((line = reader.readLine()) != null) {
            for (JCheckBox checkbox : checkboxes) {
                if (checkbox.getText().equals(line)) {
                    checkbox.setSelected(true);
                    break;
                }
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年5月17日 14:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定