问题
further to my question Java JFilechooser. It was suggested to extend BasicFileChooserUI, overriding create/getModel and providing an implementation of BasicDirectoryModel.
I attempted this however, I could not achieve it.
JFileChooser does not have a setUI method. So your only choice is to override 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;
}
};
and
public class CustomFileChooserUI extends BasicFileChooserUI
{
public CustomFileChooserUI(JFileChooser b)
{
super(b);
}
@Override
protected void createModel()
{
// TODO Auto-generated method stub
super.createModel();
}
}
but i get exceptions. Please help
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)
回答1:
JFileChooser does have a setUI method to override. JFileChooser is a subclass of JComponent which has that method. It's signature is setUI(ComponentUI).
I've updated my answer to include a simple application to show off setting a custom UI delegate for my special subclass of a file chooser. It assumes you are running under the Windows L&F so if you are not you will need to update the subclass of the file chooser to extend the proper base UI delegate. Avoid using BasicFileChooserUI otherwise you won't see anything.
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);
}
}
And here's the custom file chooser class.
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();
}
}
}
来源:https://stackoverflow.com/questions/6874181/java-jfilechooser-customization