vendredi 29 septembre 2017

How to get the status of a JCheckBox which components are in a VerticalBox?

I'm creating a script where you can choose a folder of which you can pick components via a JCheckBox and copy them to another folder. My problem is, that i worked with a VerticalBox to store the file names and added the JCheckBox into it, so i have no clue how to get the status of a single CheckBox because it's variable is overridden for each new file and I can only read it from the last listed file.

Here's the (unfinished) 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);
    }
}




Aucun commentaire:

Enregistrer un commentaire