如何在 swing java 中使用 select checklistbox 中的所有选项?

how to use select all option in checklistbox in swing java?

在我的代码片段中! "checkBoxList" 没有文件选择器选择并存储在其中的文件

复选框"tmp"它有文件的复选框!

当我在 panel.It 中显示文件 [checkboxlist] 时,显示为未选中! 在我有了 tick/untick 的选项之后。

我有以下 select/unselect 选项的代码

我需要知道何时显示文件!文件应显示为选中(打勾) 然后我可以修改我可以 tick/untick.

我坚持这个逻辑!

[

编辑:我做了并更新了这部分的答案(见图)。

我将 select/deselectall 添加到面板(框)并且有效

box.add(chckbxSelectAll);

&& 我需要并且很好奇如何将我的 selectall 复选框放入我的面板

]

public void selectAllMethod() {
Iterator<JCheckBox> i = checkBoxList.iterator();
while (i.hasNext()) {
    JCheckBox tmp = i.next();
        if (chckbxSelectAll.isSelected()) {
            tmp.doClick();
        } else {
            tmp.setSelected(false);
            selectedCounter -= 1;
            if (selectedCounter < 0) {
                selectedCounter = 0;
            }
    noOfFileTxt.setText(Integer.toString(selectedCounter));
        }
    }
}

这是我的按钮选择方法,用于选择文件夹并将其显示在带有复选框的面板中

public void chooseDirectoryFrom() {
    String tempStr = null;
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        fileChooser = new JFileChooser();
        Font font = new Font("Latha", Font.ITALIC, 10);
        fileChooser.setFont(new Font("Latha", Font.PLAIN, 13));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setFont(font);

        int returnVal = fileChooser.showOpenDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            tempStr = fileChooser.getSelectedFile().getCanonicalPath();
        }
        if (tempStr != null && !tempStr.trim().equals("")) {
            searchBox.setText(tempStr);
            // Enable the search button
        //  btnDisplay.setEnabled(true);
        } else {
            //btnDisplay.setEnabled(false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

//  public void selectToDisplay() {            //disabled btn to display
    File sourceFolder = null;
    Box box = Box.createVerticalBox();
    if (boxList.size() != 0) {
        middlePanel.remove(scrollPane);
        middlePanel.repaint();
        frame.repaint();
        boxList = new ArrayList<Box>();
        checkBoxList = new ArrayList<JCheckBox>();
        fileNamesMap = new HashMap<String, String>();
        selectedCounter = 0;
        noOfFileTxt.setText(Integer.toString(selectedCounter));
    }
    sourceFolder = new File(searchBox.getText());
    File[] sourceFilesList = sourceFolder.listFiles();
    JCheckBox cb1 = null;
    for (int i = 0; i < sourceFilesList.length; i++) {
        if (sourceFilesList[i].isFile() & sourceFilesList[i].getName().endsWith(".txt")) {
            fileNamesMap.put(sourceFilesList[i].getAbsolutePath(), sourceFilesList[i].getName());
            cb1 = new JCheckBox(sourceFilesList[i].getAbsolutePath()); 
            cb1.setFont(new Font("Latha", Font.BOLD, 20));
            box.add(cb1);
            checkBoxList.add(cb1);
            cb1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (((AbstractButton) e.getSource()).isSelected()) {
                        selectedCounter += 1;
                    } else {
                        selectedCounter -= 1;
                        if (selectedCounter < 0) {
                            selectedCounter = 0;
                        }
                    }
                    noOfFileTxt.setText(Integer.toString(selectedCounter));
                }
            });
        }
    }

    boxList.add(box);
    scrollPane = new JScrollPane(box);
    scrollPane.setPreferredSize(new Dimension(1050, 350));
   scrollPane.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
   middlePanel.add ( scrollPane );
   frame.getContentPane().add(middlePanel);
   frame.repaint();
  frame.revalidate();
}

这是我的图片(没有选择)!当我在面板中加载文件时

回答我自己的问题:

添加在for循环中选择的复选框集和 在复选框动作侦听器之外,以便它执行 setselected 方法!

 cb1.setSelected(!cb1.isSelected());
             selectedCounter += 1;
             noOfFileTxt.setText(Integer.toString(selectedCounter));



selectedCounter += 1; will display the ticked count to the textfield(noOfFileTxt)

谢谢:)