本文介绍了在JFileChooser中设置RTL滚动条的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

我该如何解决?

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


推荐答案

....我认为通过添加到你可以列出isVisible();或isDisplayable ...并提取JComponents JFileChooser (复合组件),然后调用 getMyComponents();

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

这篇关于在JFileChooser中设置RTL滚动条的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:44