Java 在JFileChooser内设置滚动条的RTL方向?
我有一个JFileChooser,我想让它从右向左,为此我使用:Java 在JFileChooser内设置滚动条的RTL方向?,java,swing,jfilechooser,right-to-left,Java,Swing,Jfilechooser,Right To Left,我有一个JFileChooser,我想让它从右向左,为此我使用: applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 但一个问题出现了。尽管JFileChooser对话框的RTL方向正确,但水平滚动条仍设置为左侧。请看这张图片: 我怎样才能修好它 以下是SSCCE: import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.sw
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
但一个问题出现了。尽管JFileChooser对话框的RTL方向正确,但水平滚动条仍设置为左侧。请看这张图片:
我怎样才能修好它
以下是SSCCE:
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);
}
@Override public String getDialogTitle()
{
return title;
}
}
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();
chooser = new MyFileChooser("aaa", "The Title");
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();
}
}
此外,我考虑了以下解决方案,但没有影响:
JScrollBar scr = new JScrollBar();
scr.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
UIManager.put("JScrollPane.ScrollBar", scr);
您尝试调用
UIManager.put(“JScrollPane.ScrollBar”,scr)代码>很好。我认为它不起作用,因为FileChooser会覆盖此设置,或者是在执行此调用之前创建的
我建议您在文件选择器被创建为JContainer之后尝试“发现”它。找到您的滚动条(或者可能是JScrollPane)并调用applycomponentation
。
我还没有尝试过这个,但我很快就会需要这样的功能,所以我很高兴知道这是否适用于您
祝你好运 。。。。我认为,通过添加到列表中,可以为isVisible()创建列表宁;或者是可显示的。。。并提取JComponentsJFileChooser
(复合组件),然后调用getMyComponents()
private void getMyComponents(){
组件findList=getJList(选择器);
JList myList=(JList)findList;
//在JList中找到文件名,并随视口视图移动到所需的矩形
组件myScrollPane=getJScrollPane(选择器);
JScrollPane scrollPane=(JScrollPane)myScrollPane;
JViewport=scrollPane.getViewport();
//使用视口视图移动到所需的矩形
}
专用组件getJList(组件组件){
if(comp.getClass()==JList.class){
返回补偿;
}
if(容器的组件实例){
组件[]组件=((容器)comp.getComponents();
对于(int i=0;i
可以是和
private void getMyComponents() {
Component findList = getJList(chooser);
JList myList = (JList) findList;
//find fileName in the JList and move with ViewPort view to the expected Rectangle
Component myScrollPane = getJScrollPane(chooser);
JScrollPane scrollPane = (JScrollPane) myScrollPane;
JViewport vport = scrollPane.getViewport();
//move with ViewPort view to the expected Rectangle
}
private Component getJList(Component comp) {
if (comp.getClass() == JList.class) {
return comp;
}
if (comp instanceof Container) {
Component[] components = ((Container) comp).getComponents();
for (int i = 0; i < components.length; i++) {
Component child = getJList(components[i]);
if (child != null) {
return child;
}
}
}
return null;
}
private Component getJScrollPane(Component comp) {
if (comp.getClass() == JScrollPane.class) {
return comp;
}
if (comp instanceof Container) {
Component[] components = ((Container) comp).getComponents();
for (int i = 0; i < components.length; i++) {
Component child = getJScrollPane(components[i]);
if (child != null) {
return child;
}
}
}
return null;
}