Java JFilechooser定制 [英] Java JFilechooser customization

查看:62
本文介绍了Java JFilechooser定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进一步回答我的问题 Java JFilechooser .建议扩展BasicFileChooserUI,覆盖create/getModel并提供BasicDirectoryModel的实现.

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没有setUI方法.因此,您唯一的选择是覆盖getUI.

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

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 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处 在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源) 在java.lang.reflect.Constructor.newInstance(未知来源) 在org.eclipse.ve.internal.java.vce.launcher.remotevm.JavaBeansLauncher.main(JavaBeansLauncher.java:86) 造成原因:java.lang.NullPointerException 在javax.swing.plaf.basic.BasicFileChooserUI $ BasicFileView.getName(未知来源)处IWAV0052E创建qwere的调用目标异常

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

JFileChooser does have a setUI method to override. JFileChooser is a subclass of JComponent which has that method. It's signature is setUI(ComponentUI).

我已经更新了答案,包括了一个简单的应用程序,以展示为文件选择器的特殊子类设置自定义UI委托的方法.假定您正在Windows L& F下运行,因此如果您不在Windows L& F下运行,则需要更新文件选择器的子类以扩展适当的基本UI委托.避免使用BasicFileChooserUI,否则您将看不到任何东西.

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

}

这篇关于Java JFilechooser定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆