4
要創建一個阿拉伯語的JFileChooser(RTL)我用下面的:創建自定義的JFileChooser
MyFileChooser:
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileView;
import java.io.File;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
public class MyFileChooser extends JFileChooser
{
private String extension;
private String title;
public MyFileChooser(String extension, String title)
{
super();
this.extension = extension;
this.title = title;
addChoosableFileFilter(new FileNameExtensionFilter(String.format("(*.%1$s) فقط %1$s ملفات", extension), extension));
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//setPreferredSize(new Dimension(450, 350));
}
@Override public String getDialogTitle()
{
return title;
}
@Override public File getSelectedFile()
{
File selectedFile = super.getSelectedFile();
if(selectedFile != null)
{
String name = selectedFile.getName();
if(!name.contains(".")) selectedFile = new File(selectedFile.getParentFile(), name + '.' + extension);
}
return selectedFile;
}
}
主營:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class MainFrame extends JFrame implements ActionListener
{
public MyFileChooser chooser;
public MainFrame()
{
super("Main Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch(Exception e){ System.out.println("Unable to load Windows look and feel");}
setPreferredSize(new Dimension(300, 100));
((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13));
setLayout(new FlowLayout());
JButton btn = new JButton("Open");
btn.setActionCommand("myButton");
btn.addActionListener(this);
add(btn);
JPanel panel = new JPanel();
UIManager.put("FileChooser.saveButtonText", "حفظ");
UIManager.put("FileChooser.openButtonText", "فتح");
UIManager.put("FileChooser.cancelButtonText", "إلغاء");
UIManager.put("FileChooser.updateButtonText", "تحديث");
UIManager.put("FileChooser.helpButtonText", "مساعدة");
UIManager.put("FileChooser.saveButtonToolTipText", "حفظ ملف");
UIManager.put("FileChooser.openButtonToolTipText", "فتح ملف");
UIManager.put("FileChooser.cancelButtonToolTipText", "إلغاء");
UIManager.put("FileChooser.updateButtonToolTipText", "تحديث");
UIManager.put("FileChooser.helpButtonToolTipText", "مساعدة");
UIManager.put("FileChooser.listViewButtonToolTipText", "قائمة");
UIManager.put("FileChooser.lookInLabelText", "بحث في:");
UIManager.put("FileChooser.newFolderToolTipText", "إنشاء مجلد جديد");
UIManager.put("FileChooser.fileNameLabelText", "اسم الملف:");
UIManager.put("FileChooser.filesOfTypeLabelText", " نوع الملف:");
UIManager.put("FileChooser.detailsViewButtonToolTipText", "تفاصيل");
UIManager.put("FileChooser.upFolderToolTipText", "للأعلى")
chooser = new MyFileChooser("aaa", "العنوان");
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileHidingEnabled(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("myButton"))
{
int status = chooser.showOpenDialog(null);
// blah blah
}
}
public static void main(String[] args)
{
new MainFrame();
}
}
截圖:
一切都很酷,除了一些地方,我需要對它們進行修改,所以我可以得到一個阿拉伯語的JFileChooser:
- 滾動條始終設置到左側時,列表視圖上。我該如何解決它?
- 在Details視圖中,表格的標題用英文寫成。我如何修改它或至少禁用細節視圖?
- 標籤「最近的項目」,「桌面」,「我的文檔」,「計算機」和「網絡」如何修改它們或至少刪除它們?
1)從提取JList和setSelectedItem :-),2)有關詳細信息,您可以在這個論壇搜索; 3)檢查此線程http://stackoverflow.com/questions/6182110/file-browser -gui by @Andrew Thompson的靈感,在這個論壇上更好的線索之一 – mKorbel