package topica.edu.vn.ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
public class HocJOptionPaneVaJFileChoose {
public HocJOptionPaneVaJFileChoose() {
// TODO Auto-generated constructor stub
initContainer();
initComponent();
addComponent();
setAction();
frame.setVisible(true);
}
private void setAction() {
// TODO Auto-generated method stub
@Override
// TODO Auto-generated method stub
XuLyThoatPhanMem();
}
});
@Override
// TODO Auto-generated method stub
XuLyMoFile();
}
});
@Override
// TODO Auto-generated method stub
XyLyLuuFile();
}
});
}
protected void XyLyLuuFile() {
// TODO Auto-generated method stub
/*
*Khi đã đọc được từ file có sẵn trong hệ thống và hiển thị nó lên JtextArea rồi
*Mình muốn chỉnh sửa và lưu nó vào file gốc. (thêm sửa xóa)
*/
if(chooser.
showSaveDialog(null)==JFileChooser.
APPROVE_OPTION) {
String data
= jtxArea.
getText(); try {
osw.write(data);
/*
* Đoạn này nếu cho thêm
* fos.close(); thì chương trình báo lỗi stream closed trong khi code mẫu vấn chạy được
* tham khảo tại sao lại có lỗi này
http://g...content-available-to-author-only...e.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/BufferedWriter.java#BufferedWriter.close%28%29
http://g...content-available-to-author-only...e.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/BufferedReader.java#BufferedReader.close%28%29
*/
osw.close();
JOptionPane.
showMessageDialog(null,
"Lưu file thành công");
// TODO: handle exception
System.
out.
println(e.
getMessage()); }
}
}
protected void XuLyMoFile() {
/*
* Ở hàm này mình muốn mở file có sẵn rồi đọc , dữ liệu sẽ được ghi ra JtextArea
* - Để những file trong danh sách mình chọn đã được lọc chỉ có file txt ta sử dụng hàm setFileFilter
*/
// TODO Auto-generated method stub
@Override
public String getDescription
() { // TODO Auto-generated method stub
return "File.TXT";
}
@Override
public boolean accept
(File f
) { // TODO Auto-generated method stub
return f.getAbsolutePath().endsWith(".txt");
}
});
/*
* Nếu bạn tiếp tục muốn lọc nhiều loại file hơn thì tiếp tục sử dụng hàm setFileFilter kết hợp với toán tử ||
*/
@Override
public String getDescription
() { // TODO Auto-generated method stub
return "Word2000,Word2010,Word2003";
}
@Override
public boolean accept
(File f
) { // TODO Auto-generated method stub
return f.getAbsolutePath().endsWith(".doc")||f.getAbsolutePath().endsWith(".docx");
}
});
if(chooser.
showOpenDialog(null)==JFileChooser.
APPROVE_OPTION) {
try
{
File slectedFile
= chooser.
getSelectedFile(); StringBuilder builer = new StringBuilder();
/*
* Thằng builer này sẽ được gán giá trị bởi thằng line (line là mỗi dòng)
* line chỉ đọc được 1 dòng rồi sau đó gán giá trị đó cho builer , tiếp tục next đến dòng tiếp theo
*/
while(line!=null)
{
builer.append(line);
line = br.readLine();
}
jtxArea.setText(builer.toString());
}
{
}
}
}
protected void XuLyThoatPhanMem() {
// TODO Auto-generated method stub
/*
* Khi chúng ta bấm thoát thì hiện ra của sổ confirm đây gọi là cửa sổ xác nhận nó sẽ trả về 1 số nguyên
* Khi nhân vào nút thoát nó sẽ xuất hiện cửa số YES_NO
* Sự kiện được xử lý khi bạn nhấn YES hoặc No (nó sẽ trả về 1 số nguyên như nói ở trên)
*/
"Bạn có chắc chắn muốn thoát",
{
}
}
private void addComponent() {
// TODO Auto-generated method stub
}
private void setMenuBar() {
// TODO Auto-generated method stub
frame.setJMenuBar(menubar);
file
= new JMenu("Hệ thống"); menubar.add(file);
file.add(FileSave);
file.add(FileOpen);
file.add(FileExit);
}
private void initComponent() {
// TODO Auto-generated method stub
setMenuBar();
/*
* Chú ý khi sử dụng JTextArea
* + Đơn giản đây chỉ là 1 trường để ta nhập text vào nhưng kết hợp với JScrollPane
* + Khi sử dụng JtextArea ta cần sử dụng lineWrap(dùng để xuống dòng) và setWrapStyleWord (Dùng để xuống dòng nhưng từ không bị chia ra)
*/
jtxArea.setLineWrap(true);
jtxArea.setWrapStyleWord(true);
}
private void initContainer() {
// TODO Auto-generated method stub
frame
= new JFrame("My Windows"); frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); frame.setSize(400, 600);
frame.setLocationRelativeTo(null);
}
public static void main
(String[] args
) { new HocJOptionPaneVaJFileChoose();
}
}