-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileProcessorGui.java
88 lines (70 loc) · 2.86 KB
/
FileProcessorGui.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class FileProcessorGui {
// class variables (GUI components)
private JFrame frame;
private JLabel label, footer;
// constructor for this class (to set up the GUI)
public FileProcessorGui() {
// configure empty panel to act as padding between GUI components on the frame
JPanel paddingPanel = new JPanel();
paddingPanel.setPreferredSize(new Dimension(400, 10));
// set window title and other configurations
frame = new JFrame("Multi-Thread File Processor");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// center the GUI window on the screen
frame.setLocationRelativeTo(null);
// add description to window
label = new JLabel("This program can process text files concurrently.");
frame.add(label);
// instantiate "Select Folder" button
JButton selectFolderButton = new JButton("Select Folder");
selectFolderButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectFolder();
}
});
// add button to frame
frame.add(selectFolderButton);
// add padding between "Select Folder" button and footer
frame.add(paddingPanel);
// instantiate footer message
footer = new JLabel("Developed by Ravindu Aratchige");
// add footer to frame
frame.add(footer);
// display GUI window
frame.setVisible(true);
}
// handle directory selection
private void selectFolder() {
// instantiate JFileChooser object
JFileChooser fileChooser = new JFileChooser();
// configure fileChooser object to select folders only, not files
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = fileChooser.showOpenDialog(frame);
if (returnValue == JFileChooser.APPROVE_OPTION) {
// instantiate java.io.File object with selected folder
File selectedFolder = fileChooser.getSelectedFile();
// get absolute path of selected folder
String directoryPath = selectedFolder.getAbsolutePath();
// start concurrently processing files in the selected folder
ConcurrentFileProcessor processor = new ConcurrentFileProcessor(directoryPath);
processor.processFiles();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// launch the GUI
new FileProcessorGui();
}
});
}
}