Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my snippet code! "checkBoxList" has no of files that are chosen by a file chooser and stored in it

check box "tmp" it has checkboxes for the files!

When i display the files[checkboxlist] in my panel.It comes as unchecked! After I have the option for tick/untick it.

I have the below code for select/unselect option

I need to know when I display the files!The files should display with checked(tick) Then I can modify which I can tick/untick.

I stuck on this logic!

[

EDIT: I did and updated answer for this part(see the image).

I Add select/deselectall to the panel(box) and it worked

box.add(chckbxSelectAll);

&& I need&curious to know how to put my selectall checkbox inside my panel

]

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));
        }
    }
}

Here is my button selection method for choosing folder and displaying it in a panel with check box

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();
}

Here is my image(without selection)!When i load the files in the panel

like image 340
Ram Avatar asked Nov 08 '22 19:11

Ram


1 Answers

To answer my own question:

Add checkbox set selected inside the for loop and outside the checkbox action listener So that it will perform setselected method!.

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



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

Thank you :)

like image 159
Ram Avatar answered Nov 14 '22 21:11

Ram