java - 在 JFileChooser 中设置滚动条的 RTL 方向?

标签 java swing jfilechooser right-to-left

我有一个 JFileChooser,我想让它从右到左定向,为此我使用:

applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

但出现了一个问题。尽管 JFileChooser 对话框的 RTL 方向正确,水平滚动条仍设置在左侧。看看这张图片:

enter image description here

我该如何解决?

这是一个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;
    }
}

主营:
    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);

最佳答案

.... 我认为通过添加 PropertyChangeListenerJFileChooser你可以监听 isVisible();或 isDisplayable ... 并提取 JComponents JFileChooser (复合成分),然后调用 getMyComponents() ;

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;
}

关于java - 在 JFileChooser 中设置滚动条的 RTL 方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724784/

相关文章:

java - JFileChooser:保存取消之前用户导航到的最后一个目录

java - 低性能计算机上的 Spring bean 单例复制

java - 在 jar 文件中执行 shell 脚本。如何提取?

java - 刷新 java Jtable 以显示更新的数据

javascript - JTEXTPANE 多彩前景设置

java - 使用 JFileChooser 将图像加载到 JLabel 图标中

java - 寻找回文的低效解决方案

java - 检查逻辑或中的所有条件

java - 删除 JButton 上的异步更新

java - 打开不带扩展名的文件 - Java