かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[Java][Swing][NetBeans]Swing Application Framework その3「Resourceを気軽に使える」

普段Javaのコードを書いてて思う。
マジックナンバーより定数。文字列なら定数よりもプロパティファイルから読み込む。
そんなのが素敵なアプリケーションの作り方だとわかっているけど、実態はめんどくさいのでハードコーディングしてしまいがちだったりする。特に、予定外のことをやるときとかにこの傾向が自分は強い。そういう傾向の人って結構いるような気がするんだけどどうだろう??

それは置いといて、Swing Application Frameworkを使ったアプリケーションを普通に開発してると、自動的にGUIに絡む部分の文字列はプロパティファイルに格納される。
すばらしい。

ということでちょびっと見てみる。
まず、ResourceStudyという名前でデスクトップアプリケーションを作る。そして、出来上がったウィンドウにラベルとボタンを置く。ボタンのアクションの設定からsayHelloというメソッドをイベントとして新規に登録する。テキストに「こんにちは」ツールチップに「こんにちはを表示する」を設定して了解をクリックする。
image 
sayHelloメソッドには、ラベルにこんにちはを表示するコードを書いておく。

    @Action
    public void sayHello() {
        jLabel1.setText("こんにちは");
    }

これで、実行してボタンを押すとラベルに「こんにちは」と表示されるアプリケーションが出来上がる。

ということで、プロパティファイルがどんな感じになってるか見てみる。
プロパティファイルは、対応するクラスの入っているパッケージのresourcesパッケージにクラス名と同じ名前で作られることになってる。
なので、この場合は、resourcestudy.ResourceStudyViewクラスに置いたラベルやボタンの情報を見たいので、resourcestudy.resources.ResourceStudyView.propertiesを見るのがいい。開いてみると、色々な文字列が書かれてるけど、最後のほうに下のような記述がある。

jLabel1.text=jLabel1
jButton1.text=jButton1
sayHello.Action.shortDescription=こんにちはを表示する
sayHello.Action.text=こんにちは

デザイナで設定した文字列等は、ここに自動的に出力されるようになっているのがわかる。
ありがたいことだ。

ただ、やっぱりというか当然のことだけど自分でプログラム中に文字列を埋め込むのはプロパティファイルに自動で記述されない。というか、勝手にされるとうざいことこの上ないだろう。
これは、残念なことに自分で手動でもってプロパティファイルに移動させないといけない。Eclipseだと、ここらへんも利ファクタリングでなんとかしてくれたような気がするが、ざっと見た感じNetBeansにそんな機能はなさそうだ。

ということで、プロパティファイルに一行追加する。

sayHello.output.message=こんにちは

そしてsayHelloメソッドを下のように書き換える。

    @Action
    public void sayHello() {
        jLabel1.setText(getResourceMap().getString("sayHello.output.message"));
    }

とまぁ、手動でやらなければならないところもあるけど、デザイナ上で指定した文字列とかはプロパティファイルに落とされるようになってるので、大分手間は軽減されるように思う。

以上!

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

Feedback

# longchamp bag 2012/12/15 15:32 http://www.longchampbagoutlet.info/category/longch

You quite know any stuff...

# foulard burberry 2012/12/15 23:05 http://www.sacburberryecharpe.fr/category/echarpe-

While using my race games and using fast music:D

# burberry uk online 2012/12/17 2:58 http://www.burberryuksale.org/category/burberry-on

Looking onward to browsing more!

# トリーバーチ新しい 2012/12/17 20:23 http://www.torybruchjp.info/category/トリーバーチ

The fashion don't collapse flat not to mention Philips doesn't produce a travel pouch within the package.

# burberry pas cher 2012/12/18 2:14 http://burberrypascher.monwebeden.fr

I so want to take a fabulous closer view some of this memorabilia!

# burberry outlet 2012/12/18 20:27 http://www.burberryoutlet2012.info/category/burber

Our admins have got a sharp eye and perhaps sharper wits - plus our Greatest Comments neighborhood enjoys an incredible read. Come play with us!

# michael kors pas cher 2012/12/19 13:26 http://sac-michael-kors.webnode.fr/blog/

My group is impressed by the quality of information for this website. There are numerous good methods here.

# Sacs Michael Kors 2012/12/21 2:40 http://sac-michael-kors.webnode.fr

If these sound very good I'd 100 % wear these at your house.

# www.tt6262.com 2013/03/04 5:03 http://tt6262.com/

A genuine colleague will be that overlooks your entire disappointments and then can handle your entire achievements. www.tt6262.com http://tt6262.com/

# spartoo 2013/03/04 23:19 http://www.c88.fr/

A friendly relationship often is the Coptis trifolia groenlandica the fact that scarves all of the hearts of all of the universe. spartoo http://www.c88.fr/

# casquette obey 2013/03/16 8:54 http://www.b77.fr/

Around the globe that you are a single person, nevertheless to at least one woman that you are the. casquette obey http://www.b77.fr/

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

Enjoy, companionship, follow, really don't unify people as much as a frequent hate with an issue. casquette new era http://www.a44.fr/

# casquette supreme 2013/03/17 6:58 http://www.b44.fr/

The very most horrible solution miss a friend or relative needs to be chilling appropriate definitely these folks comprehending one can‘h keep these things. casquette supreme http://www.b44.fr/

# destockchine 2013/03/24 0:39 http://d77.fr/

Your sister possibly the buddy, yet somehow the buddy have invariably been any sister. destockchine http://d77.fr/

# destockchine 2013/03/25 5:09 http://c99.fr/

If you decide to is likely to choosing the best hidden secret due to an enemy, see it among the somebody. destockchine http://c99.fr/

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

In which there's always bond with out romance, you will hear romance with out bond. coachoutletcoupon77.com http://www.coachoutletcoupon77.com/

# gemo 2013/04/07 9:05 http://ruezee.com/

I prefer you do not resulting from whom you are, yet somehow resulting from the people that My organization is presents 'm together with. gemo http://ruezee.com/

タイトル
名前
Url
コメント