如何添加JFileChooser从JPOPUP菜单中获取txt?

时间:2016-10-21 03:47:42

标签: java user-interface jfilechooser

我在这项任务中需要一些帮助。 点击"找到"弹出窗口....我想打开filechooser并记下文本。 这有效,但不是我想要的。

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;


 public class Notepad extends JFrame {

     JFrame n = new JFrame();
     JTextArea espacio = new JTextArea();
     JPopupMenu mix = null;
     final JEditorPane document = new JEditorPane();


     public void body(){

    setTitle("Proyecto 2");
    setBounds(360,90,500,450);


    getContentPane().setLayout(null);


    document.setBounds(1,1,499,448);
    getContentPane().add(document, null);




    document.addMouseListener(new MouseAdapter() 
            {
                public void mousePressed(MouseEvent e) 
                {
                    if(javax.swing.SwingUtilities.isRightMouseButton(e)) {

                            mix = new JPopupMenu();

                            JComponent component=(JComponent)e.getComponent();

                            ActionMap actionMap=component.getActionMap();

                            Action action=actionMap.get(DefaultEditorKit.copyAction); 

                            JMenuItem menuItem=mix.add(action);

                            menuItem.setText("Copiar");

                            mix.addSeparator();
                            action=actionMap.get(DefaultEditorKit.pasteAction);
                            menuItem=mix.add(action);

                            menuItem.setText("Pegar");

                            mix.addSeparator();
                            action=actionMap.get(DefaultEditorKit.cutAction);

                            menuItem=mix.add(action);

                            menuItem.setText("Cortar");


                            mix.addSeparator();
                            action=actionMap.get(DefaultEditorKit.selectAllAction);
                            menuItem=mix.add(action);
                            menuItem.setText("Seleccionar Todo");

                            mix.addSeparator();

                            action=actionMap.get(buscador());

                            menuItem=mix.add(action);

                            menuItem.setText("Buscar");

                            mix.show(e.getComponent(),e.getX()-4,e.getY()-6); 

                    }
                }
            });





    setVisible(true); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


     }




            private Object buscador() {
             JFileChooser find = new JFileChooser();

              int result = find.showOpenDialog(espacio);
           if (result==JFileChooser.APPROVE_OPTION) {
               File file = find.getSelectedFile();
               try {
                   document.setPage(file.toURI().toURL());
               } catch(Exception e) {
                   e.printStackTrace();
               }

            }
        return result;
            }






    public static void main(String[]args){

         Notepad n = new Notepad();
         n.body();

     }
 }

enter image description here

enter image description here

当我右键单击JFileChooser而不是弹出时。

应该打开弹出窗口,然后按"搜索"打开JFileChooser。

1 个答案:

答案 0 :(得分:0)

这是一个非常快速的答案,因为它没有为Action界面提供完整的实现,但我相信它至少会让你朝着正确的方向前进。

首先修改此代码:

action=actionMap.get(buscador());

对此:

action = buscador();

然后将您的buscador方法更改为:

private Action buscador() {
    return new Action() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser find = new JFileChooser();

            int result = find.showOpenDialog(espacio);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = find.getSelectedFile();
                try {
                    document.setPage(file.toURI().toURL());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        @Override
        public void addPropertyChangeListener(
                PropertyChangeListener listener) {
            // TODO Auto-generated method stub

        }

        @Override
        public Object getValue(String key) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isEnabled() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public void putValue(String key, Object value) {
            // TODO Auto-generated method stub

        }

        @Override
        public void removePropertyChangeListener(
                PropertyChangeListener listener) {
            // TODO Auto-generated method stub

        }

        @Override
        public void setEnabled(boolean b) {
            // TODO Auto-generated method stub

        }

    };
}