I did write a small application that catches and displays UDP messages sent via a certain port.
It works fine whith "screen dump" output (System.out.println) but if I try to send the output to a TextArea I get a compiler error "Cannot make a static reference to the non-static field TextArea1"
See code below.
How can I fix this?
Rik
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class TextAreaApplet extends Applet implements ActionListener { public void init() { Button1 = new Button("Clear"); Button1.addActionListener(this); TextArea1 = new TextArea("",5,40); TextArea1.setEditable(false); add(Button1); add(TextArea1); } public static void main(String[] args) throws IOException { String NewData; DatagramSocket ds= new DatagramSocket(2237); byte[] receive= new byte[65535]; DatagramPacket DpReceive= null; while (true) { DpReceive= new DatagramPacket(receive,receive.length); ds.receive(DpReceive); NewData= data(receive)+"\n\r"; System.out.println(NewData); // this works fine // TextArea1.append(NewData); // compiler error "Cannot make a static reference to the non-static field TextArea1" if (data(receive).toString().equals("bye")) { System.out.println("Client sent bye ... "); ds.close(); break; } receive= new byte[65535]; } } public void actionPerformed(ActionEvent e) { TextArea1.setText(""); } public static StringBuilder data(byte[] a) { if (a == null) return null; StringBuilder ret = new StringBuilder(); int i = 0; while (a[i] != 0) { ret.append((char) a[i]); i++; } return ret; } TextArea TextArea1; Button Button1; }