Configure Java JFilechooser

in addition to my Java JFilechooser question . It was proposed to extend BasicFileChooserUI, override create / getModel and provide an implementation of BasicDirectoryModel.

I tried this, however, I could not achieve this.

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.DelegatingConstructorAccessorImfn.nn. Constructor.newInstance (Unknown source) on org.eclipse.ve.internal.java.vce.launcher.remotevm.JavaBeansLauncher.main (JavaBeansLauncher.java:86) Called: java.lang.NullPointerException in javax.swing.plafb.plasb.plafbpl.baf. BasicFileChooserUI $ BasicFileView.getName (Unknown source) IWAV0052E Invocation Target qwere creation exception

 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
source share
1 answer

JFileChooser has a setUI method to override. JFileChooser is a subclass of JComponent that has this method. Its signature is setUI (ComponentUI) .

I updated my answer to include a simple application to show the custom delegate setting for my special subclass of file selection. It is assumed that you are running Windows L & F, so if you do not, you will need to update the file selection subclass to extend the corresponding main user interface. Avoid using BasicFileChooserUI, otherwise you will not 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 is the custom selection select 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(); } } } 
+1
source

All Articles