-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNotePadDemo.java
More file actions
123 lines (110 loc) · 3.15 KB
/
NotePadDemo.java
File metadata and controls
123 lines (110 loc) · 3.15 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class NotePadDemo extends JFrame {
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
private JPanel contentPane;
private void saveFile(){
String currentDirPath = "/Users/amit/Documents";
JFileChooser saveDialog = new JFileChooser(currentDirPath);
saveDialog.showSaveDialog(this);
File file = saveDialog.getSelectedFile();
String path = file.getPath();
try {
FileOperationsDemo.writeFile(path,textArea.getText());
this.setTitle(path);
} catch (IOException e) {
// TODO Auto-generated catch block
textArea.setText(e.toString());
//e.printStackTrace();
}
}
private void openFile(){
String currentDirPath = "/Users/amit/Documents";
JFileChooser openDialog = new JFileChooser(currentDirPath);
openDialog.showOpenDialog(this);
File file = openDialog.getSelectedFile();
String path = file.getPath();
try {
textArea.setText(FileOperationsDemo.readFile(path));
} catch (IOException e) {
textArea.setText(e.toString());
// TODO Auto-generated catch block
//e.printStackTrace();
}
//System.out.println(file.getPath());
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NotePadDemo frame = new NotePadDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void resizeTextArea(){
scrollPane.setSize(this.getWidth()-20,this.getHeight()-50);
textArea.setSize(this.getWidth()-20,this.getHeight()-50);
}
/**
* Create the frame.
*/
public NotePadDemo() {
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
resizeTextArea();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
scrollPane.setBounds(6, 26, 426, 266);
contentPane.add(scrollPane);
textArea.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
textArea.setBounds(6, 26, 400, 241);
scrollPane.setViewportView(textArea);
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem newItem = new JMenuItem("New");
file.add(newItem);
JMenuItem open = new JMenuItem("Open");
file.add(open);
open.addActionListener((e)->{
openFile();
});
JMenuItem save = new JMenuItem("Save");
file.add(save);
save.addActionListener((e)->{
saveFile();
});
//menuBar.setBounds(6, -8, 132, 22);
//contentPane.add(menuBar);
//contentPane.add(textArea);
}
}