-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileBrowser.java
53 lines (49 loc) · 1.58 KB
/
FileBrowser.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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class FileBrowser extends JPanel implements ActionListener {
JLabel label = new JLabel("File List");
JButton newFile = new JButton("New File");
JButton open = new JButton("Open");
JTextField newFileTF = new JTextField(10);
ButtonGroup bg;
File directory;
public FileBrowser(String dir) {
directory = new File(dir);
directory.mkdir();
JPanel fileList = new JPanel(new GridLayout(directory.listFiles().length + 3, 1));
fileList.add(label);
bg = new ButtonGroup();
for(File file : directory.listFiles()) {
JRadioButton radio = new JRadioButton(file.getName());
radio.setActionCommand(file.getName());
bg.add(radio);
fileList.add(radio);
}
JPanel newP = new JPanel();
newP.add(newFileTF);
newP.add(newFile);
newFile.addActionListener(this);
open.addActionListener(this);
fileList.add(open);
fileList.add(newP);
add(fileList);
}
@Override
public void actionPerformed(ActionEvent e) {
Login login = (Login) getParent();
if(e.getSource() == open) {
login.add(new Editor(directory.getName() + "/" + bg.getSelection().getActionCommand()), "editor");
login.cl.show(login, "editor");
}
if(e.getSource() == newFile) {
String file = directory.getName() + "/" + newFileTF.getText() + ".txt";
if(newFileTF.getText().length() > 0 && !(new File(file).exists())) {
login.add(new Editor(file), "editor");
login.cl.show(login, "editor");
}
}
}
}