private void openMenuActionPerformed(java.awt.event.ActionEvent evt) {
DBmanager db = new DBmanager();
if (!db.getCurrentUser().equals("Admin")) {
JOptionPane.showMessageDialog(this, "You are Not Allowed to Run Applications");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("MS Office Documents", "docx", "xlsx", "pptx"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
fileChooser.setAcceptAllFileFilterUsed(false);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else if (db.getCurrentUser().equals("Admin")) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(true);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}// TODO add your handling code here:
}
Xin chào .... Tôi đang cố lọc các tệp trong bộ lọc tệp bằng cách đặt fileChooser.setAcceptAllFileFilterUsed(false);
. Tùy chọn "tất cả các tệp" biến mất khỏi FileChooser
nhưng tất cả các tệp vẫn hiển thị trừ khi bạn chọn tùy chọn từ tài liệu PDF, ms Office hoặc images.I chỉ muốn có 3 bộ lọc tùy chỉnh của tôi khi mở trình chọn tệp.Cách hạn chế trình chọn tệp trong java đối với các tệp cụ thể
Sử dụng 'setFileFilter (FileFilter lọc) 'để thiết lập một bộ lọc mặc định khi nó tải. – BlackBox
Bạn đã trải qua [hướng dẫn chọn tệp] (http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html) chưa? Nó giải thích việc sử dụng bộ lọc tệp cũng như 'FileView' .. BTW - Để được trợ giúp tốt hơn sớm hơn, hãy đăng một [SSCCE] (http://sscce.org/). –
Cảm ơn ..... thêm bộ lọc mặc định đã làm việc fileChooser.setFileFilter (FileNameExtensionFilter mới ("Tài liệu PDF", "pdf")); – julihx