0

I am trying to change the contents of a JTextPane. I think the problem is scope issues. You can't compile this code but you get the idea... This may be a tough one because of the multi-threading. TYIA

C:\ucdhb2\gaia\inigui\inigui15\src\inigui13.java:259: error: local
variable theframe is accessed from within inner class; needs to be declared final
                    theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo",
null);
                    ^ C:\ucdhb2\gaia\inigui\inigui15\src\inigui13.java:259: error: cannot
find symbol
                    theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo",
null);
                                      ^   symbol:   method getStyledDocumemt()   location: variable mainfield of type JTextField
2 errors

Code

   class inigui13 extends applet  {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        JTextField mainfield;

      public void init() { 

        inigui13 theframe = new inigui13(); 
        theframe.setSize(700, 525); 
        theframe.setVisible(true); 

        try {

            echoSocket = new Socket("192.168.2.3",  9900);
                        out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream() ) );
        } catch (UnknownHostException e) {

            System.err.println("Sstemexit");
            System.exit(1);

        } catch (IOException e) {

                System.err.println("IOFailed");
                System.exit(1);
        }


        Runnable readsock = new Runnable()  {

            public void run()   {

                boolean recvread = true;

                while( recvread )   {

                    BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));

                    try {


                    theframe.mainfield.getStyledDocumemt().insertString(1, "hellooo", null);

                    } catch( IOException e ){}

                }

                  }

                    try {

                        out.close();
                        in.close();
                        echoSocket.close();
                    } catch( IOException e ){}


        }

        };


        Thread readsockt = new Thread(readsock);
        readsockt.start();
       }


       public inigui13  {

          mainfield = new JTextPane;
          this.add(mainfield);
      }
};
       }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roland Sams
  • 173
  • 1
  • 2
  • 12
  • That code makes no sense. You open and close socket, but never use it? You use a `JTextField` in the example, but reference a `JTextPane` in the question (and try and use `getStyleDocument` on a `JTextField`??). We'll be lucky if we can actually even get you close to solving your problem with that :P – MadProgrammer Nov 29 '12 at 04:13

1 Answers1

3

Do not, EVER, update, change or modify ANY UI component from any thread OTHER then the Event Dispatching Thread. This just about covers the first 10 commandments of Swing.

You code is a classic example of when to use a SwingWorker.

You might like to have read through Concurrency in Swing

Example

public class StreamReaderWorker extends SwingWorker<Object, String> {

    private JTextPane field;

    public StreamReaderWorker(JTextPane field) {
        this.field = field;
    }

    @Override
    protected void process(List<String> chunks) {
        for (String chunk : chunks) {
            try {
                field.getStyledDocument().insertString(1, "chunk", null);
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    protected String doInBackground() throws Exception {
        boolean recvread = true;
        while (recvread) {
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            try {
                publish("hellooo");
            } catch (IOException e) {
            }
        }
        return null;
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366