创建自定义 JFileChooser
要创建阿拉伯语 JFileChooser (RTL),我使用以下命令:
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;
}
}
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:
一切都很酷,除了某些部分我需要修改它们,以便我可以获得阿拉伯语 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 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;
}
}
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一个有趣的类,它自定义了 JFileChooser。它可以激励您进行更有趣的定制。看一下customizeFileChooser()方法。希望这有帮助。
below is an interesting class that customizes a JFileChooser. it can inspire you on doing more interesting customization. take a look at method customizeFileChooser(). hope this helps.