3

I have these class and its nested classes (please go to the relevant line):

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Client {
  private static final int FRAME_WIDTH = 500, FRAME_HEIGHT = 500;

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new ClientPanel());
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        frame.setVisible(true);
      }
    });
  }

  @SuppressWarnings("serial")
  private static class ClientPanel extends JPanel {
    JTextArea textArea;
    JTextField textField;
    JButton goButton;

    private ClientPanel() {
      setLayout(new BorderLayout());

      textArea = new JTextArea();
      add(textArea, BorderLayout.NORTH);

      textField = new JTextField();
      add(textField, BorderLayout.CENTER);

      goButton = new JButton("Go");
      add(goButton, BorderLayout.PAGE_END);
      goButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          textArea.append("Go button pressed. Text in field: "
              + textField.getText() + "\n ");
        }
      });
    }

    private static class GetBinaryThread extends Thread {
      private String data;

      public GetBinaryThread(String data) {
          this.data = data;
      }

      public void run() {
        try {
          ClientPanel.this.textArea.append(", recieved" + data);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
}

The compiler gives me an error on line:

ClientPanel.this.textArea.append(", recieved" + data);

No enclosing instance of the type Client.ClientPanel is accessible in scope

How am I able to access the JTextArea of the outer class?

durron597
  • 31,968
  • 17
  • 99
  • 158
Max Segal
  • 1,955
  • 1
  • 24
  • 53
  • Reopened on the grounds that the other question doesn't have the complete example that this does - we can *definitely* tell what's wrong here, and the other question doesn't make that clear. – Jon Skeet Aug 14 '15 at 20:31
  • 1
    possible duplicate of [No enclosing instance of the type is accessible in scope](http://stackoverflow.com/q/5439241/1768232) – durron597 Aug 14 '15 at 20:34

1 Answers1

4

Your nested class is a static nested class:

private static class GetBinaryThread extends Thread

Therefore it doesn't have an enclosing instance.

Get rid of static and then it'll be an inner class, with a reference to an instance of ClientPanel. Note that when you create an instance of GetBinaryThread (which you don't in the code you've shown) you'll need to have a ClientPanel reference to implicitly pass to the constructor as context.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @durron597: Where does the duplicate indicate that the OP's nested class was declared as `static`, and that that was the problem? It's easy to overlook unless it's pointed out. I'd argue that it would be more useful to close the *other* question as a duplicate of *this* one as this one provides more context. – Jon Skeet Aug 14 '15 at 20:38
  • @durron597: I agree that there's more code here than would be ideal, but it's not *that* much to sort through (116 lines) - I've seen a *lot* worse, and the question does also include the error message and where it occurs. Much better than far too little code, as is the case in the other question. – Jon Skeet Aug 14 '15 at 20:42
  • I'm sure you have seen much worse. I removed a lot of the junk and some of the more obvious swing mistakes. – durron597 Aug 14 '15 at 20:55