欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

为文件选择器添加右键上下文菜单,选择器上下文, public c

来源: javaer 分享于  点击 48191 次 点评:86

为文件选择器添加右键上下文菜单,选择器上下文, public c


    public class ContextMenuFileChooser extends JFileChooser {       protected Component right_click_pane;       public ContextMenuFileChooser() {           super();           JPopupMenu popup = new JPopupMenu();           popup.add(new DeleteAction(this));           popup.add(new NewFolderAction(this));           right_click_pane = new RightClickGlassPane(this,popup) {        protected void redispatchMouseEvent(MouseEvent e, boolean repaint) {                    Component component = getRealComponent(e.getPoint());             if(component == null) { return; }             String chooser_class =                "javax.swing.plaf.metal.MetalFileChooserUI$5";            if(component.getClass().getName().equals(chooser_class)) {                super.redispatchMouseEvent(e,repaint);            } else {                doDispatch(e);            }        }    };       }    protected JDialog createDialog(Component parent) {        JDialog dialog = super.createDialog(parent);        // create the right-click glass pane.        dialog.setGlassPane(right_click_pane);        right_click_pane.setVisible(true);        return dialog;    }    class DeleteAction extends AbstractAction {        protected JFileChooser chooser;        public DeleteAction(JFileChooser chooser) {            super("Delete");            this.chooser = chooser;        }        public void actionPerformed(ActionEvent evt) {            File file = chooser.getSelectedFile();            if(file != null) {                file.delete();                chooser.rescanCurrentDirectory();            }        }    } class NewFolderAction extends AbstractAction {    protected JFileChooser chooser;    public NewFolderAction(JFileChooser chooser) {        super("New Folder");        this.chooser = chooser;    }    public void actionPerformed(ActionEvent evt) {        File cwd = chooser.getCurrentDirectory();        if(cwd != null) {             File new_dir = new File(cwd,"New Folder");             new_dir.mkdir();             chooser.rescanCurrentDirectory();        }    }}    }      public static void main(String[] args) {        final JFileChooser jfc = new ContextMenuFileChooser();        jfc.showOpenDialog(null);        System.exit(0);    }
相关栏目:

用户点评