かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[NetBeans]Swing Application Framework その5「JTableも使ってみよう」

別にSwing Application Frameworkに限った話じゃないと思うけど、JTableを使ってみようと思う。昔は、TableModelをがりがりと実装して使った記憶がある。最近もそうなんだろうか?と思ってみてみると、NetBeansを使う限りだと、めっちゃ簡単にとりあえず使えるようになってるくさい。

ということで早速実験。プロジェクトを新規作成して、JTableEduという名前をつける。もちろんJavaデスクトップアプリケーションで作る。アプリケーションクラスはcom.wankuma.kazuki.jtable.JTableEduAppという名前にしておいた。
後は、ビーンバインディングも使うのでライブラリに追加しておく。

画面を作る前に、画面に表示するためのデータの入れ物!JavaBeansを1つ作っておこうと思う。
クラス名はPersonにして、下のようにいくつかプロパティを定義しておく。後で、コーディングしやすいようにコンストラクタもいくつか定義しておく。

package com.wankuma.kazuki.jtable;

import java.util.Date;

public class Person {
    private int id;
    private String name;
    private Date birthday;
    private int salary;

    public Person() {
    }

    public Person(int id, String name, Date birthday, int salary) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }
    
    // Getter/Setterは省略
}

そして、JTableEduViewにList<Person>型のプロパティを追加する。プロパティにはArrayListやLinkedListではなくて、バインド専門のリストのインスタンスを指定する。
ただし、素直にnewして作るんじゃなくてObservableCollections.observableList(List<E> list)というメソッドを使ってインスタンスを作る。とりあえず、空のリストでいいのでnew ArrayList()を入れておいた。
多分、素直にnewして作るんじゃなくて、こんなファクトリメソッドになってるのは、DBからとってきたリストを渡す前提なんだと思われる。

    private List<Person> people = ObservableCollections.observableList(new ArrayList<Person>());
    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    }

次は、デザイナ上からJTableを置いて、さっき作ったpeopleプロパティとバインドする。
バインドは、デザイナのJTableの右クリックメニューから「表の定義」を選択すると開くカスタマイザダイアログからやる。
image

カスタマイザダイアログの表のモデルタブから、バウンドを選んでFormのpeopleを選択する。ちょうど上の画像のような感じになる。
ついでに、Fileメニューの下にMenuを1つ追加してアクションを追加する。
image

アクションの名前は、addPersonにした。
image

addPersonメソッドは、単純にpeopleに対してPersonを1つ追加するだけのシンプルなものです。

    @Action
    public void addPerson() {
        people.add(new Person(jTable1.getRowCount(), "田中 変太郎", new Date(), 1000000));
    }

次に、カスタマイザダイアログの列タブで、挿入ボタンを押し捲ってPersonクラスのプロパティの数だけ列を追加する。
image

これで実行すると、メニューのAdd Personを押すたびに表にデータが追加されていくプログラムが出来上がる。
image

これを、DBからとってくるようにすれば、お手軽DB編集プログラムが出来そうだ。というかJavaデスクトップアプリケーションのDBアプリの雛形がまさにそうだったりする。
このコードを見てるだけでも勉強になる。暇なときに眺めてみよう。

投稿日時 : 2008年6月23日 23:32

Feedback

# welded ball valve 2012/10/18 21:26 http://www.jonloovalve.com/Full-welded-ball-valve-

I like this web blog so much, saved to fav. "Nostalgia isn't what it used to be." by Peter De Vries.

# Nike Air Max 90 Mens 2012/12/08 1:14 http://superairmaxshoes1.webs.com/

Only wanna admit that this is very helpful , Thanks for taking your time to write this.

# sac longchamp pliage 2012/12/14 20:45 http://www.saclongchampachete.com/category/sac-lon

While performing my off road racing games and taking note of fast new music:D

# longchamp pas cher 2012/12/15 15:41 http://www.saclongchampachete.info/category/longch

The only those that would look good sporting these fugly things might be Ferrari hole crew within the pits:D

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

We are impressed by the quality of information during this website. There are plenty of good options here.

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

Its chance are so fantastic along with working trend so immediate.

# longchamp bags 2012/12/17 20:42 http://www.longchampbagoutlet.info/category/longch

You genuinely know a person's stuff...

# michael kors paris sac 2012/12/18 20:03 http://michael-kors-canada.webnode.fr/blog/

Go as a result of these aged items and just look for your that tickle a person's fancy.

# sacs Burberry 2012/12/19 13:34 http://sacburberrysoldesfr.webnode.fr

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

# longchamp le pliage 2012/12/21 3:39 http://longchampukbags.webeden.co.uk

make these people red with a yellow indy!!

# http://michael-kors-canada.webnode.fr/news-/ 2012/12/22 17:38 http://michael-kors-canada.webnode.fr/

I in fact like em. It's dislike you have on these out walking around town most people.

# ZbgfTDaCjvcueicx 2014/07/17 22:18 http://crorkz.com/

7K7xwz Great article post. Really Great.

# ivermectin over the counter canada 2021/09/28 16:16 MarvinLic

ivermectin 1mg http://stromectolfive.com/# stromectol coronavirus

# ivermectin 3 2021/11/01 7:59 DelbertBup

generic ivermectin cream http://stromectolivermectin19.com/# п»?ivermectin pills
ivermectin 400 mg brands

# ivermectin cream cost 2021/11/03 6:38 DelbertBup

cost of ivermectin pill http://stromectolivermectin19.online# ivermectin price canada
ivermectin 5 mg

# cheap generic ed pills 2021/12/04 15:48 JamesDat

https://genericpillson.com/# cheap generic pills dapoxetine

# sildenafil citrate tablets 100 mg 2021/12/07 15:39 JamesDat

https://iverstrom24.online/# stromectol ivermectin drug

# bimatoprost generic 2021/12/11 17:54 Travislyday

https://bimatoprostrx.com/ bimatoprost ophthalmic solution careprost

# bimatoprost buy 2021/12/12 12:39 Travislyday

http://stromectols.online/ stromectol 12mg

# bimatoprost generic best price 2021/12/13 8:26 Travislyday

http://baricitinibrx.online/ baricitinib coronavirus

# bimatoprost buy https://bimatoprostrx.com
careprost bimatoprost for sale
2021/12/13 16:10 Hksfnjkh

bimatoprost buy https://bimatoprostrx.com
careprost bimatoprost for sale

# ivermectin australia 2021/12/17 9:48 Eliastib

seplho https://stromectolr.com stromectol 3mg cost

# order doxycycline online https://doxycyline1st.com/
online doxycycline 2022/02/26 17:51 Jusidkid

order doxycycline online https://doxycyline1st.com/
online doxycycline

# Prescription Drug Information, Interactions & Side. Read now.
https://edonlinefast.com
Long-Term Effects. Best and news about drug. 2023/02/18 2:28 EdOnline

Prescription Drug Information, Interactions & Side. Read now.
https://edonlinefast.com
Long-Term Effects. Best and news about drug.

# canada medications online https://pillswithoutprescription.pro/# 2023/05/15 5:18 PillsPresc

canada medications online https://pillswithoutprescription.pro/#

タイトル
名前
Url
コメント