Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with MouseListener

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Please help me with one thing.
I want to perform a series of actions and display the final result.
Something like
Performing first action.......... succeded
Performing second action.......... succeded
Performing third action.......... succeded
Performing fourth action.......... failed
Each of these actions lasts nearly 2-3 seconds.
When I click a button (that begins doing all that and that has been added an ActionListener to)
they all go through and the results display after the last action is completed.
I tried adding a MouseListener.
Something like this

String result=null;
public void mousePressed(MouseEvent me){
"Performing first action........";
result = DoMy2-3SecondslastingMethod();
}
public void mouseReleased(MouseEvent me){
DisplayResult(result); //succeded or failed
}
It works when I perform just one action.
But how can switch between them?
In a word I need this
1. Display "Performing first action..........". Carry out the first action
2. Display the result of the first action then pass to the next.
I need to do all that by clicking a button once.
How can this be done?
Please help!
Thank you!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can perform your actions in sequence like this:

The tricky bit is showing the result on a Swing UI. If you code something to update a text box inline with your operations it won't update the UI reliably. And if you do everything in the mouse click event the UI will be "frozen" until all the actions are done. This may or may not be A Bad Thing.

The solution is to run your actions on another thread. If you haven't seen threads yet this may look a little scary ... don't worry we can talk through them.

That gets our actions on their own thread so they don't lock up the UI. But I still left "show result" very vague. See if you can build this much that just shows results by writing to the console.

To put the result on the GUI we'll use SwingUtilities to run an update command.

ViewUpdater is yet another class that uses the run() method to update one of the controls on the UI with the results. I won't go into any detail on that yet because first I want to ask ...

Is this making any sense at all? I can't tell from here if you're nodding your head and eating all this up or just wondering what the heck that guy is talking about. How we doin?
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
Frankly speaking all that I do is sending SMS messages through a phone
connected to my PC.
On a single click upon a button I need to send from 32 to 37 messages.
I have a method SendSMS with string return value (Sent or Failed).
I have a JTable consisting of two columns <Recipient, Status>

In the Receipent column I display smth like
Sending to +99450XXXXXXX............
Then String rslt = SendSMS(Receipent, Message);
and in the corresponding row of the Status column display "rslt".
Then with the 2nd message to do the same.
When doing a sequence (for ex. 3 recipients ) like the following
(on a button or mouse click)
String rslt = null;

table.setValueAt("Sending to +994501111111.............", 0, 0);
rslt = SendSMS(Receipent, Message);
table.setValueAt(rslt, 0, 1);

table.setValueAt("Sending to +994502222222.............", 1, 0);
rslt = SendSMS(Receipent, Message);
table.setValueAt(rslt, 1, 1);

table.setValueAt("Sending to +994503333333.............", 2, 0);
rslt = SendSMS(Receipent, Message);
table.setValueAt(rslt, 2, 1);

everything works fine.
But I have 32 receipents.
When I add it into a cycle like
for(int i=0; i<=2; i++){

table.setValueAt("Sending to" + Receipent[i].............", i, 0);
rslt = SendSMS(Receipent[i], Message[i]);
table.setValueAt(rslt, i, 1);

}
as you said the results display after all three actions are completed.
And the reason is clear. This happens becasue the listener listens for all the actions to complete.
But I dont want to do it by means of Threads.
PLEASE HELP!
Looking forward to your reply!
Thank you!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a Swing giant but I'm pretty sure the issue is that when you set values into your table the Swing thread does not get any cycles to repaint the screen before you go on to the next send. If so, you have to get your heavy work off the Swing thread and post your updates back to the screen via SwingUtilities.invokeWhatever. I had to do exactly this in one of the three or four Swing programs I've done.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!