2017-09-29 12 views
1

Je crée un script dans lequel vous pouvez choisir un dossier dans lequel vous pouvez sélectionner des composants via un JCheckBox et les copier dans un autre dossier. Mon problème est, que j'ai travaillé avec un VerticalBox pour stocker les noms de fichiers et ajouté le JCheckBox dedans, donc je n'ai aucune idée comment obtenir le statut d'un CheckBox simple parce que sa variable est substituée pour chaque nouveau fichier et je peux seulement lire à partir du dernier fichier répertorié.Comment obtenir l'état d'un JCheckBox quels composants sont dans un VerticalBox?

est ici le (inachevé) Code:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 

import javax.swing.Box; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 

public class CopyGUI extends JFrame implements ActionListener { 

    private static final long serialVersionUID = 1L; 
    private Box box; 
    private File fromFile; 
    private File toFile; 
    private File folder; 
    private File[] listOfFiles; 
    private JButton beginButton; 
    private JButton fromButton; 
    private JButton toButton; 
    private JCheckBox checkBox; 
    private JLabel fromLabel; 
    private JLabel toLabel; 
    private JPanel panel; 
    private JScrollPane scrollPane; 
    private JTextField fromField; 
    private JTextField toField; 

    public CopyGUI() { 
     init(); 
    } 

    private void init() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setBounds(200, 200, 420, 700); 
     setResizable(false); 
     setTitle("File Copy"); 

     panel = new JPanel(null); 
     add(panel); 

     fromLabel = new JLabel("From:"); 
     fromLabel.setBounds(40, 20, 50, 20); 

     fromField = new JTextField(); 
     fromField.setBounds(100, 20, 200, 20); 

     fromButton = new JButton("Browse"); 
     fromButton.setBounds(300, 20, 80, 20); 
     fromButton.addActionListener(this); 

     toLabel = new JLabel("To: "); 
     toLabel.setBounds(40, 40, 50, 20); 

     toField = new JTextField(); 
     toField.setBounds(100, 40, 200, 20); 

     toButton = new JButton("Browse"); 
     toButton.setBounds(300, 40, 80, 20); 
     toButton.addActionListener(this); 

     panel.add(fromLabel); 
     panel.add(fromField); 
     panel.add(fromButton); 

     panel.add(toLabel); 
     panel.add(toField); 
     panel.add(toButton); 

     beginButton = new JButton("Begin Copy"); 
     beginButton.setBounds(280, 620, 100, 20); 
     beginButton.addActionListener(this); 

     panel.add(beginButton); 

     scrollPane = new JScrollPane(); 
     scrollPane.setBounds(40,80,340,520); 

     box = Box.createVerticalBox(); 

     scrollPane = new JScrollPane(box); 
     scrollPane.setBounds(40,80,340,520); 

     panel.add(scrollPane); 
    } 

    public void actionPerformed(ActionEvent e) { 

     if (e.getSource() == fromButton) { 
      JFileChooser fileChooser = new JFileChooser(); 
      fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 

      int op = fileChooser.showOpenDialog(this); 
      if (op == JFileChooser.APPROVE_OPTION) { 
       fromFile = fileChooser.getSelectedFile(); 
       fromField.setText(fromFile.getAbsolutePath() + "\\"); 
       folder = new File(fromFile.getAbsolutePath()); 
       listOfFiles = folder.listFiles(); 
      } 

      box.removeAll(); 

      for (int i = 0; i < listOfFiles.length; i++) { 
       if (listOfFiles[i].isFile()) { 
        checkBox = new JCheckBox(listOfFiles[i].getName()); 
        box.add(checkBox); 
       } 
       else if (listOfFiles[i].isDirectory()) { 
       } 
      } 

      panel.revalidate(); 
      panel.repaint(); 
     } 

     if (e.getSource() == toButton) { 
      JFileChooser fileChooser = new JFileChooser(); 
      fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 

      int op = fileChooser.showOpenDialog(this); 
      if (op == JFileChooser.APPROVE_OPTION) { 
       toFile = fileChooser.getSelectedFile(); 
       toField.setText(toFile.getAbsolutePath() + "\\"); 
      } 
     } 

     if (e.getSource() == beginButton) { 

      System.out.println(checkBox.isSelected()); 

     } 
    } 

    public static void main(String[] args) { 
     new CopyGUI().setVisible(true); 
    } 
} 

Répondre

0

Vous aurez besoin de garder référence à chaque JCeckBox vous ajoutez:

JCheckBox checkBox = new JCheckBox(listOfFiles[i].getName()); 
    box.add(checkBox); 

    //you could keep reference in a List such as 
    List<JCheckBox> listOfChkBox = new ArrayList<>(); //class variable 
    listOfChkBox.add(checkBox); 

    //or a map 
    Map<String,JCheckBox> mapOfChkBox = new HashMap<>(); //class variable 
    mapOfChkBox.add(listOfFiles[i].getName(), checkBox);