java 将 ActionListener 添加到 JTextArea 的解决方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3902553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:54:58  来源:igfitidea点击:

Workaround for adding ActionListener to JTextArea

javaswingactionlistenerjtextarea

提问by mastodon

I have a program that get's input string with file path in one JTextArea and then loads it's content to a second JTextArea. Problem is that when using JTextArea I cannot add an actionListener that will load content in the second JTextArea when leaving this field. How to get around this problem ?

我有一个程序,它在一个 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)
}

回答by I82Much

You do not want an actionListener, you want a FocusListener.

您不需要 actionListener ,而是需要FocusListener

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

});

回答by Andrew Thompson

Or, to flesh out my first comment, try this SSCCE that uses a JButton (& a JEditorPane for the content).

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

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

回答by Enyby

If you need only ActionListener, check this example:

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

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

Its equals:

它等于:

textArea.addActionListener(actionListener);

P. S. actionListener must be final or class field for use in that way.

PS actionListener 必须是 final 或 class 字段才能以这种方式使用。