Kenji Tachibana's Weblog

« 前の日(Dec月 20日, 2008年) | 日付別メイン | 次の日(Dec月 22日, 2008年) »

http://blogs.sun.com/kenji/date/20081222 2008年 12月 22日 月曜日

ハンバーガー大好き

突然ですが、ハンバーガー、大好きです。小さいころから結構好きでしたが、特に好きになったのは、まだ入社して間もないころ、半年間アメリカに出張していたのがきっかけです。ともかく、何を食べてもまずくて、しかも、お金も大してない私には、Burger King が、主食でした。ハンバーガーばかり食べていることを現地の日本の方に告げると、「もっとおいしいところがあるよ」といろいろ連れて行ってもらい、ハンバーガーっておいしいく、奥が深いなーと感じるようになりました。で、日本に帰ってきてからも、ちょくちょくおいしいというお店に食べにいきます。おりしもハンバーガーブームらしいですし (もう去ったかな?)



CoCoChi


神奈川県横浜市青葉区藤が丘2-3-1 桂ビル1F


オーダー: アボガドバーガー


コメント:
これも近所にあるハンバーガー屋さんです。できた当初は、「これはうまい!」と感動しました。パンがほんのり甘く、お肉はしっかり、ジューシー。アボガドとの相性も抜群で、遠くまで行かなくても、こんなハンバーガーが食べられるとは幸せっと、結構よく足を運んでいました。ところが... 小麦が高いといわれてしばらくしていってみたのですが、うーん、なんかパンが変わってます。気のせいかもしれませんが、普通のパンになってました。たまたまかも知れません。でも、それ以来、足が遠のいてしまって... もう一度、いってみてもいいですね。


評価: ★★★ (以前の味なら、5つ星なんですけどね...)

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.



Implement of CustomerLoadImpl class (Java)


For preparing the database side, please use the same process as my previous blog (How to connect Data Base from JavaFX UI with NetBeans), from step 1 - step5.
After that, I need to add one more class to use Async from JavaFX, called CustomerLoadImpl inherited from AbstractAsyncOperation class.

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;
}
}

This class will be called from JavaFX side, and create Customer object by using JPA controller. This should be done in asynchronous. When this CustomerLoadImpl object is created and inherited start() method is invoked, call() method (override) is called and find the Customer object with input Id value, and put it in the attribute. Then, to be able to get this value, create getter method.
To compile this class, please add javafxrt.jar in the Library. In my env, install NetBeans 6.5 first, and add JavaFX as plugin, it is in C:\Users\tkenji\.netbeans\6.5\javafx-sdk1.0\lib\shared.

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);
}
}

This CustomerLoad object is created, call start() object of CustomerLoad (Java side) . Then, when the action is finished, call onCompletion() method. In this case, start() method in CustomerLoad create new object of CustomerLoadImpl, and execute start() method of CustomerLoadImpl. This method will invoke call() method of CustomerImpl as described before. onComplte() method is a function handed as the one of the args of CustomerLoad class constructor, and execute the defined function.

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();
}
}
}

For onCoplete function, put the Customer Name from DB to CustomerName value. This value is binded to Text UI and the name is displayed when the value is changed.

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 .


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.