2017-09-29 3 views
1

私は、JCheckBoxを介してコンポーネントを選択して別のフォルダにコピーできるフォルダを選択できるスクリプトを作成しています。私の問題は、私はVerticalBoxを使ってファイル名を保存し、JCheckBoxを追加したので、新しいファイルごとに変数がオーバーライドされて読み込みができるため、単一のCheckBoxのステータスを取得する方法がわからないそれは最後にリストされたファイルからです。JCheckBoxのステータスをどのように取得するにはどのコンポーネントがVerticalBoxにありますか?

ここで(未完)コードです:

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

答えて

0

あなたが追加した各JCeckBoxへの参照を保持する必要があります:

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); 
関連する問題