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

发布于 2024-11-24 06:27:19 字数 3032 浏览 2 评论 0原文

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

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

I have a JFileChooser and I want to make it oriented from right-to-left, and to do so I use:

applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

but an issue shows up. The horizontal scrollbar is still set to the left despite the JFileChooser dialog is oriented RTL correctly. Look at this image:

enter image description here

How can I fix it?

Here is an 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();
        }
    }

Also, I thought about the following solution but it didn't affect:

JScrollBar scr = new JScrollBar();
scr.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
UIManager.put("JScrollPane.ScrollBar", scr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-12-01 06:27:20

您尝试调用 UIManager.put("JScrollPane.ScrollBar", scr); 效果很好。我认为它不起作用,因为 FileChooser 覆盖了此设置或在执行此调用之前创建。

我建议您在将文件选择器创建为 JContainer 后尝试“发现”它。找到滚动条(或者可能是 JScrollPane)并调用 applyComponentOrientation
我还没有尝试过这个,但我很快就会需要这样的功能,所以我很高兴知道这是否适合您。

祝你好运。

Your attempt to call UIManager.put("JScrollPane.ScrollBar", scr); is good. I think that it did not work because the FileChooser overrides this setting or is created before you perform this call.

I'd suggest you to try to "discover" the file chooser after it has been created as a JContainer. Find your scroll bar (or probably JScrollPane) and call applyComponentOrientation.
I have not tried this yet but I will need such functionality soon, so I'd be glad to know whether this is working for you.

Good luck.

梦途 2024-12-01 06:27:19

....我认为通过将 PropertyChangeListener 添加到 < a href="http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html" rel="nofollow">JFileChooser 您可以收听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;
}

.... I think that by adding PropertyChangeListener to the JFileChooser you can listnening for isVisible(); or isDisplayable ... and extract JComponents JFileChooser (compound component) and then call 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文