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?

2

1 Answer 1

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.

3
  • 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, 2015 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, 2015 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, 2015 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.