将ActionListener添加到JTextArea的解决方法

时间:2010-10-10 22:51:33

标签: java swing actionlistener jtextarea

我有一个程序在一个JTextArea中获取带有文件路径的输入字符串,然后将其内容加载到第二个JTextArea。问题是当使用JTextArea时,我无法添加一个actionListener,它将在离开此字段时在第二个JTextArea中加载内容。如何解决这个问题?

protected JTextArea inputField, outputField;

public Main(){
    super(new BorderLayout());
    inputField = new JTextArea(5, 20);
    outputField = new JTextArea(2, 20);
    //inputField.addActionListener(this);
    inputField.setEditable(false);
    JScrollPane scroller2 = new JScrollPane(inputField);
    JScrollPane scroller1 = new JScrollPane(outputField);

    this.add(scroller1, BorderLayout.WEST);
    this.add(scroller2, BorderLayout.EAST);
}

public void actionPerformed(ActionEvent evt) {
    String text = inputField.getText();
    (loading contents of file)
}

3 个答案:

答案 0 :(得分:7)

您不需要actionListener,您需要FocusListener

JTextArea text = ...;
text.addFocusListener(new FocusListener() {
    public void focusGained(FocusEvent e) {}
    public void focusLost(FocusEvent e) {
        // Load your content.
    }

});

答案 1 :(得分:2)

或者,为了充实我的第一个评论,试试这个使用JButton的SSCCE(& a JEditorPane作为内容)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import java.io.File;

class LoadDocument {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                final JFrame f = new JFrame();
                f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

                JPanel contentPane = new JPanel( new BorderLayout(3,3) );
                contentPane.setBorder( new EmptyBorder(5,5,5,5) );

                // has convenience methods to load documents..
                final JEditorPane content = new JEditorPane();
                JScrollPane sp = new JScrollPane(content);
                sp.setPreferredSize( new Dimension(400,400) );
                contentPane.add( sp, BorderLayout.CENTER );

                final JFileChooser jfc = new JFileChooser();

                JButton open = new JButton("Open File");
                open.addActionListener( new ActionListener(){
                        public void actionPerformed(ActionEvent ae) {
                            int result = jfc.showOpenDialog(f);
                            if (result==JFileChooser.APPROVE_OPTION) {
                                File file = jfc.getSelectedFile();
                                try {
                                    content.setPage( file.toURI().toURL() );
                                } catch(Exception e) {
                                    e.printStackTrace();
                                    JOptionPane.showMessageDialog(
                                        f,
                                        "File load error!",
                                        e.getMessage(),
                                        JOptionPane.ERROR_MESSAGE
                                        );
                                }
                            }
                        }
                    } );
                JToolBar tb = new JToolBar();
                tb.add(open);
                contentPane.add( tb, BorderLayout.NORTH );

                f.setContentPane( contentPane );
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

答案 2 :(得分:1)

如果您只需要ActionListener,请查看以下示例:

textArea.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
        actionListener.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "focusLost"));
    }
});

等于:

textArea.addActionListener(actionListener);

P上。 S. actionListener必须是final或class字段才能以这种方式使用。