かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Java][Swing][NetBeans]Swing Application Framework その4「Resourceをもっと活用」

さて、前回は、デザイナ上で指定した文字列とかが自動的にプロパティファイルに格納されるのを見た。
そのほかにも、便利?な機能がSwing Application Frameworkにあるので簡単に見てみようと思う。

とりあえず、前回のResourceStudyというプロジェクトのresourcestudyパッケージにPersonクラスを新規作成する。Personクラスには、nameプロパティとageプロパティを追加する。そして、フィールドに@Resourceアノテーションを追加する。これをつけておくことで、プロパティファイルから値を設定することができるようになる。

package resourcestudy;

import org.jdesktop.application.Resource;

public class Person {
    @Resource
    private String name;
    @Resource
    private int age;

    // Getter/Setterは省略
}

そして、ResourceStudyViewのプロパティとしてPersonクラスを追加する。

    private Person person;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

まだ、インスタンスを作る処理を書いてないけど、この年齢と名前を表示するように画面をデザインしておく。「名前:」「年齢:」と項目を表示するラベル2個と、実際に名前と年齢を表示するラベル2個の合計4個を置く。名前と年齢を表示するラベルは、バインドでFormのpersonプロパティのnameとageをバインドする。

ラベルを右クリックして、バインド → textを選択する。
image

出てきたウィンドウで、Formを選択してpersonのname(年齢のほうはageにする)を選択する。
image

Swing Application Frameworkは、クラスのプロパティをプロパティファイルから読み込む機能がデフォでついてる。
具体的には、ApplicationContextクラスのgetResourceMapメソッドの引数にクラスを指定して呼び出すと、そのクラスの所属するパッケージ+resource + クラス名.propertiesから自動的にResourceMapを作ってくれる。
ApplicationContextクラスはApplication.getApplication().getContext()で取得できる。FrameViewを継承しているクラスでは、ApplicationクラスはgetApplication()で取得できるので、下のようなコードでResourceMapが取得できることになる。

        ResourceMap personResourceMap = getApplication().getContext().getResourceMap(Person.class);

そして、ResourceMapのinjectFieldsメソッドを呼ぶと、プロパティファイルの値を引数で渡したオブジェクトのフィールドに代入してくれる。もちろん最初につけたResourceアノテーションのあるものに限ってだけど。ということで、コードは以下のようになる。

    public ResourceStudyView(SingleFrameApplication app) {
        super(app);

        ResourceMap personResourceMap = getApplication().getContext().getResourceMap(Person.class);
        person = new Person();
        personResourceMap.injectFields(person);

        initComponents();
        
        // 以下略
    }

肝心のプロパティファイルの中身だけど、クラス名.フィールド名 = 値といった感じで設定していく値を列挙する。

Person.name = 田中 一郎
Person.age = 18

これで準備完了。実行してみると、personオブジェクトにデータが設定されて、その値がラベルに表示される。
image

いい感じだ。

投稿日時 : 2008年6月22日 23:47

Feedback

# le pliage longchamp outlet 2012/12/14 20:45 http://www.sacslongchamp2012.info

I be aware of everybody should hate built in, but I don't even think they seem so undesirable.

# sac longchamp soldes 2012/12/15 16:09 http://www.soldesacslongchamp.info/category/longch

It's RIGHT. You can always be a dailymotion star =)

# sacs longchamp france 2012/12/16 17:59 http://www.sacslongchamp2012.info/le-pliage-longch

I apply earbuds away from home because with their portability, even though I favor over a ear.

# women burberry bag 2012/12/17 2:58 http://www.burberryuksale.org/category/bags-burber

Its option are thus fantastic in addition to working form so immediate.

# michael kors pas cher 2012/12/18 20:51 http://sacmichaelkorssoldes.monwebeden.fr

Very useful info. Hope to work out more threads soon!

# burberry bags online 2012/12/18 21:18 http://www.burberryoutlet2012.info/category/burber

The only individuals who would glance good being dressed in these fugly things is Ferrari hole crew within the pits:D

# http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/ 2013/01/13 4:56 http://www.destockchinefr.fr/veste-marque-pas-cher

Friendships continue at the time each single pal believes that brand-new areas such as a small favorable position covering the more.
http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/ http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/

# www.b66.fr 2013/02/27 8:50 http://www.b66.fr/

Appreciate may be fragile with commence, even so gets bigger a lot more as they age if at all sufficiently raised on. www.b66.fr http://www.b66.fr/

# F77.fr 2013/03/03 14:44 http://www.f77.fr/

One particular buddie is almost certainly not companion, yet companion will always be any buddie. F77.fr http://www.f77.fr/

# destock mode 2013/03/16 8:54 http://www.b77.fr/

Take pleasure in might well known worry towards days additionally , the increase of truley what many of us absolutely love. destock mode http://www.b77.fr/

# casquette ny 2013/03/16 10:02 http://www.a44.fr/

Real love can be the activated consideration on your lifetime and development of what all of us really enjoy. casquette ny http://www.a44.fr/

# usine23 2013/03/24 0:39 http://e55.fr/

The particular undesirable technique miss out people thought of as chilling straight definitely these businesses understanding you can‘r ask them to. usine23 http://e55.fr/

# coachoutletcoupon55.com 2013/04/06 1:37 http://www.coachoutletcoupon55.com/

No need to to understand might be happy to be with. To understand that will stress people to jimmy you together. coachoutletcoupon55.com http://www.coachoutletcoupon55.com/

# promolaredoute.com 2013/04/07 6:02 http://www.promolaredoute.com/

Inside success our family find out all of; when misfortune we know our family. promolaredoute.com http://www.promolaredoute.com/

# brandalley 2013/04/07 12:01 http://rueree.com/

Around the world you are a single person, but nonetheless , to one individual you are all of us. brandalley http://rueree.com/

# coach factory outlet coupons 2013/04/07 14:45 http://www.coachoutletcoupon77.com/

Will not look down on, virtually all may dismal, because can never predict who's removal motivated by all your grin. coach factory outlet coupons http://www.coachoutletcoupon77.com/

# Laredoute 2013/04/07 19:00 http://ruezee.com/

Friendly relationship would be the golden thread whom scarves these kisses of all the so-called entire world. Laredoute http://ruezee.com/

# coach factory outlet online 2013/04/07 22:55 http://www.coachfactoryoutlet77.com/

Wear‘tonne squander your with a human being/partner,what individuals isn‘tonne willing to squander most of the point upon you.

# tati 2013/04/08 0:34 http://ruenee.com/

I enjoy you do not considering yourself, yet somehow considering who exactly Quite possibly once i have always been away with you. tati http://ruenee.com/

# chaussea 2013/04/08 5:39 http://ruemee.com/

Will be hang around the instant each single family member considers he's got a slight transcendency within the various other. chaussea http://ruemee.com/

# coachoutletonline33.com 2013/04/08 17:02 http://www.coachoutletonline33.com/

Rarely ever look down on, even if that you're sorrowful, reside do not no who is responsible for plummeting gets interested your actual satisfaction. coachoutletonline33.com http://www.coachoutletonline33.com/

タイトル
名前
Url
コメント