进一步回答我的问题Java JFilechooser 。建议扩展 BasicFileChooserUI,重写 create/getModel 并提供 BasicDirectoryModel 的实现。
我尝试了这一点,但是我无法实现它。
JFileChooser 没有 setUI 方法。所以你唯一的选择就是重写 getUI。
JFileChooser blah = new JFileChooser()
{
CustomFileChooserUI asdf = null;
/**
*
*/
private static final long serialVersionUID = 1L;
public FileChooserUI getUI()
{
if (asdf == null)
{
asdf = new CustomFileChooserUI(this);
}
return asdf;
}
};
和
public class CustomFileChooserUI extends BasicFileChooserUI
{
public CustomFileChooserUI(JFileChooser b)
{
super(b);
}
@Override
protected void createModel()
{
// TODO Auto-generated method stub
super.createModel();
}
}
但我遇到了异常(exception)。请帮忙
java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.eclipse.ve.internal.java.vce.launcher.remotevm.JavaBeansLauncher.main(JavaBeansLauncher.java:86) Caused by: java.lang.NullPointerException at javax.swing.plaf.basic.BasicFileChooserUI$BasicFileView.getName(Unknown Source)IWAV0052E Invocation Target Exception creating qwere
at javax.swing.JFileChooser.getName(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI$DirectoryComboBoxRenderer.getListCellRendererComponent(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source)
at javax.swing.JList.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.setListSelection(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.access$300(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.itemStateChanged(Unknown Source)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI$DirectoryComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI$DirectoryComboBoxModel.addItem(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI$DirectoryComboBoxModel.access$900(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI.doDirectoryChanged(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI.access$1200(Unknown Source)
at javax.swing.plaf.metal.MetalFileChooserUI$5.propertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.awt.Component.firePropertyChange(Unknown Source)
at javax.swing.JFileChooser.setCurrentDirectory(Unknown Source)
at javax.swing.JFileChooser.<init>(Unknown Source)
at javax.swing.JFileChooser.<init>(Unknown Source)
at qwere$1.<init>(qwere.java:12)
最佳答案
JFileChooser 确实有一个要重写的 setUI 方法。 JFileChooser 是具有该方法的 JComponent 的子类。它的签名是setUI(ComponentUI)。
我已经更新了我的答案,添加了一个简单的应用程序来展示为文件选择器的特殊子类设置自定义 UI 委托(delegate)。它假设您在 Windows L&F 下运行,因此如果不是,您将需要更新文件选择器的子类以扩展正确的基本 UI 委托(delegate)。避免使用 BasicFileChooserUI,否则您将看不到任何内容。
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileChooserUIExample extends JFrame {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileChooserUIExample();
}
});
}
public FileChooserUIExample() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Show the file chooser");
final JFileChooser chooser = new MyCustomFileChooser();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(FileChooserUIExample.this);
}
});
getContentPane().add(button, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
这是自定义文件选择器类。
import com.sun.java.swing.plaf.windows.WindowsFileChooserUI;
import javax.swing.JFileChooser;
public class MyCustomFileChooser extends JFileChooser {
public MyCustomFileChooser() {
super();
setUI(new CustomFileChooserUI(this));
}
public class CustomFileChooserUI extends WindowsFileChooserUI {
public CustomFileChooserUI(JFileChooser b) {
super(b);
System.out.println("Woohoo! I'm using a custom UI delegate!");
}
@Override
protected void createModel() {
// TODO Auto-generated method stub
super.createModel();
}
}
}
关于Java JFilechooser 定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6874181/