Pregunta

Tengo un programa que la cadena de entrada del GET con la ruta del archivo en un JTextArea y luego lo carga de contenidos a un segundo JTextArea. El problema es que cuando se utiliza JTextArea no puedo agregar un actionListener que cargar contenido en el segundo JTextArea al salir de este campo. Cómo solucionar este problema?

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)
}
¿Fue útil?

Solución

Usted no quiere un actionListener, usted quiere un FocusListener .

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

});

Otros consejos

O, para dar cuerpo a mi primer comentario, probar este SSCCE que utiliza un JButton (y un JEditorPane para el contenido).

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

Si sólo necesita ActionListener, y comprueba este ejemplo:

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

Sus iguales:

textArea.addActionListener(actionListener);

P. S. actionListener campo debe ser final o clase para el uso de esa manera.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top