[Java swing] JFileChooser trong Java
[Java swing] JFileChooser trong Java Tháng Bảy 30, 2014 nguyenvanquan7826 TUT Java swing Leave a response JFileChooser trong java là một đối tượng hiển thị khung cho phép bạn mở hoặc lưu file. Nó như thế này: Bây giờ ta sẽ ...
[Java swing] JFileChooser trong Java
JFileChooser trong java là một đối tượng hiển thị khung cho phép bạn mở hoặc lưu file. Nó như thế này:

Bây giờ ta sẽ đi làm một ví dụ đơn giản về việc mở và lưu file.

package nguyenvanquan7826.JFileChooser;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* --------------------- @author nguyenvanquan7826 ---------------------
* ------------------ website: cachhoc.net -------------------
* ---------- date: Jul 29, 2014 - filename: DemoJFileChooser.java ----------
*/
public class DemoJFileChooser extends JFrame implements ActionListener {
public static String iconOpen = "iconOpen.png";
public static String iconSave = "iconSave.png";
private JTextArea taLog;
private JButton btnOpen, btnSave;
private JFileChooser fc = new JFileChooser();
public DemoJFileChooser() {
addContent();
setDisplay();
}
/**
* set display for JFrame
*/
private void setDisplay() {
setTitle("Demo JFileChooser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* add content for JFrame, JFrame contain a ButtonPane (contain buttons) and
* a JTextArea to show something you work
*/
private void addContent() {
setLayout(new BorderLayout());
add(createButtonPanel(), BorderLayout.PAGE_START);
add(new JScrollPane(taLog = createTextArea(15, 30)),
BorderLayout.CENTER);
}
/**
* create a JPanel contain 2 button (open and save) on TOP of JFrame
*/
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(btnOpen = createButton("Open file", iconOpen));
panel.add(btnSave = createButton("Save file", iconSave));
return panel;
}
/**
* create JTextArea
*/
private JTextArea createTextArea(int row, int col) {
JTextArea ta = new JTextArea(row, col);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
return ta;
}
/**
* create a JButton with text and icon and add Action for it
*/
private JButton createButton(String text, String iconLink) {
ImageIcon icon = createImageIcon(iconLink);
JButton btn = new JButton(text, icon);
btn.addActionListener(this);
return btn;
}
/**
* get a icon via link to icon
*/
private ImageIcon createImageIcon(String iconLink) {
ImageIcon icon = new ImageIcon(getClass().getResource(iconLink));
return icon;
}
/**
* Handle when you open a file
*/
private void openFile() {
int select = fc.showOpenDialog(this);
if (select == JFileChooser.APPROVE_OPTION) {
taLog.append("You open " + fc.getSelectedFile().getName());
} else {
taLog.append("You cancelled open!");
}
taLog.append("
");
}
/**
* Handle when you save a file
*/
private void saveFile() {
int select = fc.showSaveDialog(this);
if (select == JFileChooser.APPROVE_OPTION) {
taLog.append("You save " + fc.getSelectedFile().getName());
} else {
taLog.append("You cancelled save!");
}
taLog.append("
");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOpen) {
openFile();
return;
}
if (e.getSource() == btnSave) {
saveFile();
return;
}
}
public static void main(String[] args) {
new DemoJFileChooser();
}
}
Trong code trên bạn để ý 2 lệnh để hiển thị theo mở và lưu file là khác nhau.
int select = fc.showOpenDialog(this);
int select = fc.showSaveDialog(this);
Tiếp theo đối số JFileChooser.APPROVE_OPTION thể hiện là bạn đã chấp nhận “Open” hoặc “Save” file.
Các icon save và open bạn đặt trong cùng package như sau:

Ví dụ trên chỉ mô tả về cách sử dụng JFileChooser, việc đọc và ghi file text hoặc đọc theo object bạn tự làm nhé.
Đọc thêm: use JFileChooser, class JFileChooser, java swing