I have created JAVA GUI that will receive text file(.txt) from other client and append into the textarea(by using TextRecv.append) in Java Netbeans GUI however it doesn’t read file that already sent from other client , I try to put readfile method in button perform however it read only when I push the button but that’s not the result that I want, my target is make this GUI to dynamically receive file , read and append on to my text area automatically
My situation is I have 2 Machine and then I send file by execute dtncpd in Java that’s process how I send file over 2 machines however in both 2 machine the text file is already appear in destination but It not append in the textarea
That’s how I send
private void DTNSendActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String text = "";
String name = usernameField.getText();
text = name.toString()+ ":"+ TextInput.getText().toString();
try{
FileWriter fstream = new FileWriter("msg.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(text);
out.close();
}catch(IOException e)
{
e.printStackTrace();
}
TextInput.setText("");
try{ //Show what sender type
BufferedReader ins = new BufferedReader(new FileReader("msg.txt"));
String Stream;
while((Stream = ins.readLine())!=null)
{
TextRecv.append(Stream+"\n");
}
}catch(IOException e){ e.printStackTrace();}
String sendcmd = "dtncp NetBeansProjects/DTNChat/msg.txt dtn://abc.dtn";
Runtime runtime1 = Runtime.getRuntime();
try{
Process process1 =runtime1.exec(sendcmd);
}catch(IOException e)
{
e.printStackTrace();
}
The question is how can the Java Gui read the file that receive and append to the textarea automatically without using any button
Any Suggestion?? Thank you for every advice
Edited
I’m coding like this however it’s not append on my TextArea
SwingWorker worker = new SwingWorker<Void,String>(){
protected Void doInBackground(){
File file = new File("/home/XXX/Desktop/incoming/ABC.dtn/msg.txt");
boolean exists = file.exists();
while(exists)
{
try{
FileReader read = new FileReader("/home/XXX/Desktop/incoming/ABC.dtn/msg.txt");
BufferedReader in = new BufferedReader(read);
String show;
while((show=in.readLine())!=null)
{
TextRecv.append(show);
}
}catch(IOException e){}
}
return null;
}
};
public void actionPerformed(ActionEvent ae){
worker.execute();
}
1. I am assuming that if there are 2 machines involved, then you must be sending and receiving on both sides.
2. So you need to keep a separate thread, that will be running Out of your the EDT (Event Dispatcher Thread) which is your GUI thread.
3. You can make this thread keep checking the Number of files in the particular folder where you are dropping the files, and keep a Count variable to keep the count. And Store the File Names in an ArrayList.
4. When a new file arrives, the Count will increment by One, then check whether any file with this newly arrived file name is already in your ArrayList or not, if not append this file content to your JTextArea.
5. Keep a separate thread to do the above, Not on the EDT, else your GUI will be non-responsive.
///////////////////////////////// EDITED: ////////////////////////////////////
6. The
main()method in Swing is Not Long Lived. It schedules the construction of GUI in the Event Dispactcher Thread and then quits. Now its the responsibility of the EDT to handle the GUI.7. Your main() method should only do the work of making the JFrame visible, using EventQueue.invokeLater.
Eg:
8.SwingWorker is provided by Java to Synchronize the Work Output of the Non-UI thread on the GUI thread.
/////////////////////EDITED//////////////////////////
9. You can create a thread by extending to Thread Class or by Implementing Runnable.
I am showing the example of Runnable here.
Put these 2 classes in the same file.
Class that implements Runnable
Class that contains the main() method
10. You can also use Anonymous Inner class, that implements Runnable.
Class with Anonymouse Inner class and main() method