2008年 12月 22日 月曜日
ハンバーガー大好き
突然ですが、ハンバーガー、大好きです。小さいころから結構好きでしたが、特に好きになったのは、まだ入社して間もないころ、半年間アメリカに出張していたのがきっかけです。ともかく、何を食べてもまずくて、しかも、お金も大してない私には、Burger King が、主食でした。ハンバーガーばかり食べていることを現地の日本の方に告げると、「もっとおいしいところがあるよ」といろいろ連れて行ってもらい、ハンバーガーっておいしいく、奥が深いなーと感じるようになりました。で、日本に帰ってきてからも、ちょくちょくおいしいというお店に食べにいきます。おりしもハンバーガーブームらしいですし (もう去ったかな?)
Posted at 01:21午前 12 22, 2008 by kenji in 食道楽 |
Access DB from JavaFX with NetBeans - use Async
This is English version of DB access from JavaFX.
I've got a few comments for my previous blog for about DB access.
Those comments are completely correct and when I added animation with previous animation, it became freeze while toplink was accessing to database. So, I searched how to use AsyncCallback, and found the answer in Clarkeman's blog . So, I've revised my previous code by following Clarkeman's code (Thank you, Clarkeman!)
As Clarkeman also said in his blog, JavaFX UI is invoked in the single EDT (Event Dispatch Thread), so if I kick the heavy process from JavaFX UI, other process is stopped until that task is finished. To avoid this, need to access DB in asynchronous by using com.sun.javafx.runtime.async package.
package javaapplication1;
import com.sun.javafx.runtime.async.AbstractAsyncOperation;
import com.sun.javafx.runtime.async.AsyncOperationListener;
public class CustomerLoadImpl extends AbstractAsyncOperation {
private Customer customer;
private int Id;
public CustomerLoadImpl(Customer customer, int Id, AsyncOperationListener listener) {
super(listener);
this.customer = customer;
this.Id = Id;
}
@Override
public Object call() throws Exception {
CustomerJpaController test2 = new CustomerJpaController();
this.customer = test2.findCustomer(Id);
return(this.customer);
}
public Customer getCustomer(){
return this.customer;
}
}
Then, create JavaFX side, to create asynchronous thread and get the result from CustomerLoadImpl. Before go forward, please confirm to finish the step 6 - 7, except SwingButton part (for here, add the new way to access database).
At first, create CustomerLoad class with inherited AbstractAsyncOperation class as follow.
public class CustomerLoad extends AbstractAsyncOperation {
var peer: CustomerLoadImpl;
public-init var customer:Customer;
public var onComplete: function(customer: Customer): Void;
public override function cancel() : Void {
if (peer != null) then peer.cancel();
}
protected override function start() : Void {
peer = new CustomerLoadImpl(customer, CustomerId, listener);
peer.start();
}
protected override function onCompletion(value : Object) : Void {
this.customer = peer.getCustomer();
onComplete(customer);
}
}
The last thing is, to define the SwingButton action. When the button is clicked.
var tmpcustomer = new Customer();
var test = CustomerLoad {
customer: tmpcustomer
onComplete: function(cus: Customer) : Void {
if (cus != null) {
CustomerName = cus.getName();
}
}
}
The all implementation is finished, and I confirmed that the application is accessing the DB and get the correct value. But, not sure this is actually working or not... So, to confirm this, I add an animation, which is described in GUI tutorial , and try to access DB again with this animation. And the result is... seems to be working fine. The animation is not freeze. I also tried the same way in previous db access way, but the animation is freeze correctly
.

Whole source code for JavaFX side is here .
Posted at 12:14午前 12 22, 2008 by kenji in JavaFX の素人 |