1

I'm trying to add an Document Listener to a JTextArea, so that when a user presses enter it will read in the last line / the characters since the new line.

I tried the following code, but Eclipse doesn't like display.getDocument().addDocumentListener(new AL());. It says

No enclosing instance of type ScrollingTextArea is accessible. Must qualify the allocation with an enclosing instance of type ScrollingTextArea (e.g. x.new A() where x is an instance of ScrollingTextArea).

How can I add an Action Listener to a JTextArea?

Code:

package guis;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ScrollingTextArea {

    public static void main ( String[] args )
    {
        JPanel middlePanel = new JPanel ();
        middlePanel.setBorder(new TitledBorder(new EtchedBorder (), "Display Area" ) );

        // create the middle panel components

        JTextArea display = new JTextArea(16,58 );
        JScrollPane scroll = new JScrollPane(display);

        display.getDocument().addDocumentListener(new AL());
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        //Add Textarea in to middle panel
        middlePanel.add(scroll);

        JFrame frame = new JFrame();
        frame.add( middlePanel );
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public class AL implements DocumentListener {


        @Override
        public void insertUpdate(DocumentEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            // TODO Auto-generated method stub

        }
    }
}
cubecubed
  • 228
  • 4
  • 18
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:11

2 Answers2

3

The nested class, AL, needs to be static. Unless you need to export the class for some reason, it should probably be private.

private static class AL implements DocumentListener {…}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Apr 05 '14 at 17:08
2

Create a separate class for AL

Or

Define AL class outside ScrollingTextArea class without public modifier.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Aren't the two solutions you stated the same? – cubecubed Apr 05 '14 at 17:10
  • 1
    In first case you can access `AL` class from any where regardless of package but in second case it is accessible in same package. – Braj Apr 05 '14 at 17:13
  • Oh, I see. If you made a separate class for AL it would have to be public, where as if you defined `AL` outside of `ScrollingTextArea` it could not have a `public`/`private`/`protected` modifier. – cubecubed Apr 05 '14 at 17:13