-
Notifications
You must be signed in to change notification settings - Fork 0
/
slip27B.java
60 lines (49 loc) · 2.02 KB
/
slip27B.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class FileListApp extends JFrame {
private JTextField directoryField;
private JList<String> fileList;
private DefaultListModel<String> listModel;
public FileListApp() {
setTitle("File List");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
// Create a TextField to input the directory path
directoryField = new JTextField();
panel.add(directoryField, BorderLayout.NORTH);
// Create a Button to list files and directories
JButton listButton = new JButton("List Files");
listButton.addActionListener(new ListButtonListener());
panel.add(listButton, BorderLayout.CENTER);
// Create a List Control to display the file names
listModel = new DefaultListModel<>();
fileList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(fileList);
panel.add(scrollPane, BorderLayout.SOUTH);
add(panel);
}
private class ListButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String directoryPath = directoryField.getText();
listModel.clear();
File directory = new File(directoryPath);
if (directory.exists() && directory.isDirectory()) {
File[] files = directory.listFiles();
for (File file : files) {
listModel.addElement(file.getName());
}
} else {
JOptionPane.showMessageDialog(FileListApp.this, "Invalid directory path or directory does not exist.");
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
FileListApp fileListApp = new FileListApp();
fileListApp.setVisible(true);
});
}
}