-1

I am trying to display results based on an event on the text area. These results are event driven.

jbtnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
File f = new File(installer);
if(f.exists()){
      area.setText("File exists...");
}else{
      area.setText("File not found!");
}
area.append("\n"+installer);
installer1=installer.concat("\\Autoplay");
area.append("\n"+installer1);
File f1 = new File(installer1);
if(f1.exists()){
      area.append("\nF1 File exists");
}else{
      area.setText("F1 not found!");
}
Abhishek
  • 1
  • 3
  • Could you paste your code? – Bono Mar 03 '15 at 12:23
  • Why, 'without timer'? *"I am trying.."* For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Mar 03 '15 at 12:29
  • Please check my above code and suggest.. – Abhishek Mar 03 '15 at 13:14
  • 1
    *"Please check my above code and suggest.."* Who are you replying to? Tip: Add @Bono (or whoever, the `@` is important) to notify the person of a new comment. I guess it is not me, since an uncompilable code snippet is not an MCVE or SSCCE abd you did not answer my question. – Andrew Thompson Mar 04 '15 at 06:02
  • Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Mar 04 '15 at 06:03
  • @AndrewThompson : Sure. I am very new to stackoverflow and i did not know the indentation and now i have learnt it. Going forward i will make sure the code snippet will have a proper indentation – Abhishek Mar 04 '15 at 10:06
  • *"Going forward i will make sure the code snippet will have a proper indentation"* Umm... when? In case you did not realize, your own question can be [edited](http://stackoverflow.com/posts/28831625/edit) at any time. – Andrew Thompson Mar 04 '15 at 10:08
  • ..and you ***still*** have not answered my question. Again, it is *Why, 'without timer'?* I do not ask these questions for my own amusement. They are intended to help **you** find the best solution. But your unwillingness (or inability) to answer is hampering the process. – Andrew Thompson Mar 04 '15 at 10:10

2 Answers2

1
installer1=installer.concat("\\Autoplay");

This appears to be a long running task, so you need to execute this code in a separate Thread. The easiest way to do this is to use a SwingWorker. You can then update the text area when the SwingWorker is done.

Read the section from the Swing tutorial on Concurrency for more information and SwingWorker examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

If both parts of the message are in one TextArea: part them via .substring(start, end). Then print the first part in the way you want to, for example System.out.println(). Then use wait(2000) and then print the second part.

If both texts are in different textareas, just put their content in different variables.

Oh, and the content of the textarea can be returned by .getText().

KJaeg
  • 698
  • 3
  • 7
  • 23
  • Yes I am printing in one TextArea. If i use wait(2000) This is what i get EventDemo.java:113: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown – Abhishek Mar 03 '15 at 12:28
  • *"YesEventDemo.java:113: unreported exception.."* Your point being? – Andrew Thompson Mar 03 '15 at 12:30
  • 1
    *"Then use wait(2000) and then print the second part."* It is (quite) likely that both strings will appear after the `wait(..)`. Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. Except of course, that the OP has ruled out the easy way of going about things.. – Andrew Thompson Mar 03 '15 at 12:37
  • Right, I didn't think so far. Sure the GUI will freeze. Didn't think about, that the author wants to run the GUI like before, during the wait. For that case the waiting should occur asynchronous. Isn't SwingWorker supposed for asynchronous tasks like that? – KJaeg Mar 03 '15 at 14:11
  • @Pgmr: The top answer of this post could also be interesting for you and your problem: http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep – KJaeg Mar 03 '15 at 14:14