Frage

Ich habe ein Programm, dass get die Eingabezeichenfolge mit Dateipfad in einem JTextArea und dann lädt sie den Inhalt auf einen zweiten JTextArea. Das Problem ist, dass, wenn JTextArea verwendet, kann ich keine Action hinzufügen, die Inhalte in der zweiten JTextArea geladen werden kann, wenn dieses Feld zu verlassen. Wie dieses Problem umgehen?

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)
}
War es hilfreich?

Lösung

Sie wollen nicht eine Action, Sie wollen ein Focuslistener .

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

});

Andere Tipps

Oder, um meinen ersten Kommentar Fleisch aus, versuchen Sie diese SSCCE, die eine JButton (& einen JEditorPane für den Inhalt) verwendet wird.

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

Wenn Sie nur Action, lesen Sie in diesem Beispiel:

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

Die equals:

textArea.addActionListener(actionListener);

P. S. Action muss auf diese Weise für den Einsatz Feld endgültig oder Klasse sein.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top