How to use select all option in checklist in swing java?

In my code code! "checkBoxList" has no files that are selected by the file selection and stored in it

the "tmp" flag has flags for the files!

When I display the [checkboxlist] files in my panel. He comes as out of control! After I have the option to mark / undo.

I have the code below for the select / unselect option

I need to know when I show files! Files should be displayed with a checkmark (checked) Then I can change what I can mark / uncheck.

I followed this logic!

[

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

I add select / deselectall to the panel (field) and it works

box.add(chckbxSelectAll); 

& & I need and am interested to know how to set the selectall flag 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 method of selecting buttons for selecting a folder and displaying it in a checkbox

 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 (no choice)! When I upload files to the panel

+5
source share
1 answer

To answer my own question:

Add a set of flags selected inside the for loop and outside the receiver of the flag action. So it will execute the setselected method !.

  cb1.setSelected(!cb1.isSelected()); selectedCounter += 1; noOfFileTxt.setText(Integer.toString(selectedCounter)); selectedCounter += 1; will display the ticked count to the textfield(noOfFileTxt) 

Thanks:)

+1
source

Source: https://habr.com/ru/post/1243646/


All Articles