Creating custom JFileChooser

To create an Arabic JFileChooser (RTL) I use the following: **MyFileChooser:** import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileView; import; 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; } } **Main:** 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(); } } **ScreenShots:** ![enter image description here][1] ![enter image description here][2] Everything is cool, except some parts I need to modify them so I can get an arabic JFileChooser: - The scrollbar is always set to the left when list view is on. How can I fix it? - On Details view, the header of the table is written in English. How can I modify it or at least disable the details view? - The labeles "Recent Items", "Desktop", "My Documents", "Computer", and "Network" how can I modify them or at least remove them? [1]: [2]:
1) extract JList from that and setSelectedItem :-), 2) for more details you can search on this forum, 3) check this thread by @Andrew Thompson for inspirations, one of better threads on this forum about that

以上就是Creating custom JFileChooser的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准