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:
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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…