簡體   English   中英

將ActionListener添加到JTextArea的解決方法

[英]Workaround for adding ActionListener to 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)
}

你不想要一個actionListener,你想要一個FocusListener

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

});

或者,為了充實我的第一個評論,試試這個使用JButton的SSCCE(和內容的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);
    }
}

如果只需要ActionListener,請檢查以下示例:

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

它等於:

textArea.addActionListener(actionListener);

PS actionListener必須是final或class字段才能以這種方式使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM