アプリでタスクを一覧表示する(SpringLayout,スクロール機能の実装)- Javaでデスクトップトップアプリ(予定&ToDo管理)を作る-028
今回はToDoアプリでタスクを一覧表示する機能を作っていきます。
LinkedList ・ Map ・ SpringLayout ・ ScrollPaneといった技術を使用しますので、
使い方がわからない場合は以下のページを参照してください。
LinkedList
Map
SpringLayout
ScrollPane
TaskViewクラスの新設
前回作成したTaskViewクラスを作り直して、ToDoアプリのプロジェクトに加えます。
大きな変更点としては、
コンストラクタでTaskを受け取り、Taskを保持しておく
チェックボックスなどのGIS部品をフィールドに保持する
それでは、コードを見ていきましょう。
[TaskView.java](新設)
import java.awt.*;
import javax.swing.*;
public class TaskView {
JPanel panel;
Task task;
JCheckBox ck;
JButton button;
TaskView(Task task){
int startyear,startmonth,startdate,starthour,startmin;
String taskname,category,context;
this.task=task;
this.panel = new JPanel();
this.panel.setPreferredSize(new Dimension(700,60));
this.panel.setBackground(Color.WHITE);
SpringLayout layout = new SpringLayout();
this.panel.setLayout(layout);
this.ck = new JCheckBox();
taskname = task.getName();
JLabel taskNameLabel = new JLabel(taskname);
layout.putConstraint(SpringLayout.NORTH, ck, 15, SpringLayout.NORTH, this.panel);
layout.putConstraint(SpringLayout.WEST, ck, 20, SpringLayout.WEST, this.panel);
layout.putConstraint(SpringLayout.NORTH, taskNameLabel, -5, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.WEST, taskNameLabel, 20, SpringLayout.EAST, ck);
this.panel.add(ck);
this.panel.add(taskNameLabel);
if(task.getTaskProperty().getProperty(PropertyName.STARTYEAR.toString())!=null){//STARTYEARがnullでない時,日付ラベルを表示
startyear=(int)task.getTaskProperty().getProperty(PropertyName.STARTYEAR.toString());
startmonth=(int)task.getTaskProperty().getProperty(PropertyName.STARTMONTH.toString());
startdate=(int)task.getTaskProperty().getProperty(PropertyName.STARTDATE.toString());
JLabel dateLabel = new JLabel(String.format("%4d/%02d/%02d",startyear,startmonth+1,startdate));
layout.putConstraint(SpringLayout.NORTH, dateLabel, 12, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.WEST, dateLabel, 20, SpringLayout.EAST, ck);
this.panel.add(dateLabel);
}
if(task.getTaskProperty().getProperty(PropertyName.STARTHOUR.toString())!=null){//STARTHOURがnullでない時,時間のラベルを表示
starthour=(int)task.getTaskProperty().getProperty(PropertyName.STARTHOUR.toString());
startmin=(int)task.getTaskProperty().getProperty(PropertyName.STARTMINUTE.toString());
JLabel hourLabel = new JLabel(String.format("%02d:%02d",starthour,startmin));
layout.putConstraint(SpringLayout.NORTH, hourLabel, 12, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.WEST, hourLabel, 100, SpringLayout.EAST, ck);
this.panel.add(hourLabel);
}
if(task.getTaskProperty().getProperty(PropertyName.CATEGORY.toString())!=null){//CATEGORYがnullでない時,カテゴリーラベルを表示
category=(String)task.getTaskProperty().getProperty(PropertyName.CATEGORY.toString());
JLabel categoryLabel = new JLabel(category);
categoryLabel.setPreferredSize(new Dimension(100,16));
layout.putConstraint(SpringLayout.NORTH, categoryLabel, 12, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.WEST, categoryLabel, 150, SpringLayout.EAST, ck);
this.panel.add(categoryLabel);
}
if(task.getTaskProperty().getProperty(PropertyName.CONTEXT.toString())!=null){//CONTEXTがnullでない時,コンテクストラベルを表示
context=(String)task.getTaskProperty().getProperty(PropertyName.CONTEXT.toString());
JLabel contextLabel = new JLabel(context);
layout.putConstraint(SpringLayout.NORTH, contextLabel, 12, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.WEST, contextLabel, 250, SpringLayout.EAST, ck);
contextLabel.setPreferredSize(new Dimension(100,16));
this.panel.add(contextLabel);
}
this.button = new JButton("編集");//Task編集用のボタンを作成
layout.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, ck);
layout.putConstraint(SpringLayout.EAST, button, -30, SpringLayout.EAST, this.panel);
this.panel.add(button);
}
JPanel getPanel(){return this.panel;}
}
MainFrameにviewメソッドを追加する
MainFrameにviewメソッドというものを作ります。
このメソッドは「タスクプロパティとパネルを渡すと、タスクプロパティにマッチするタスクを検索し、受け取ったパネルに検索したタスクを詰めて返してくれる」というメソッドです。
[MainFrame.java]
/* 今までのコード */
public void view(Map pr,JPanel panel){
int n=0;//Taskを数える用の変数
JPanel outer = new JPanel();//TaskViewを詰める用のパネル
SpringLayout layout1 = new SpringLayout();//ScrollPaneを設定するためのレイアウト
outer.setLayout(layout1);
SpringLayout layout2 = new SpringLayout();//更新ボタンを設置するためのレイアウト
panel.setLayout(layout2);
JScrollPane scroll=new JScrollPane(outer) ;
TaskProperty matching = new TaskProperty(pr);
List matchingTasks = Main.box.search(matching);//boxのサーチメソッドでタスクを検索
LinkedList taskView = new LinkedList();//taskView用のコレクションを用意
if(!matchingTasks.isEmpty()){
for(Iterator i = matchingTasks.iterator(); i.hasNext();){
Task task = (Task) i.next();
taskView.add(new TaskView(task));
if(n>0){
layout1.putConstraint(SpringLayout.NORTH, taskView.get(n).getPanel(), 1, SpringLayout.SOUTH, taskView.get(n-1).getPanel());//前のタスクの1px下に配置
}
outer.add(taskView.get(n).getPanel());
n=n+1;
}
}else{return;}
outer.setPreferredSize(new Dimension(700,61*(n)));//タスクの数に合わせてパネルのサイズを大きくする
scroll.setPreferredSize(new Dimension(710,480));
JButton update= new JButton("更新");
layout2.putConstraint(SpringLayout.SOUTH, update, -10, SpringLayout.SOUTH, panel);//更新ボタンの位置設定
layout2.putConstraint(SpringLayout.EAST, update, -10, SpringLayout.EAST, panel);
panel.add(scroll);
panel.add(update);
return;
}
MainFrameにactionPerformedメソッドを書き換える
MainFrameには「今日」、「明日」、「スケジュール」、「いつか」、「完了済み」のパネルがあります。
サイドボタンのアクションコマンドによってswitch文で分岐します。今回は「今日」のパネルの部分だけ実装してみます。
[MainFrame.java]
/* 今までのコード */
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();//アクションコマンドを取得
if(cmd==null){
return;
}else{
switch(cmd){
case "Today" :
Calendar calendar = Calendar.getInstance();//今日の日付を取得
Map mtProperty = new HashMap();
//「今日」のタスクを検索するためのプロパティをつくる
mtProperty.put(PropertyName.STARTYEAR.toString(), calendar.get(Calendar.YEAR));
mtProperty.put(PropertyName.STARTMONTH.toString(), calendar.get(Calendar.MONTH));
mtProperty.put(PropertyName.STARTDATE.toString(), calendar.get(Calendar.DATE));
mtProperty.put(PropertyName.STARTWEEK.toString(), calendar.get(Calendar.WEEK_OF_YEAR));
mtProperty.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty,this.todayModePanel);
mtProperty.clear();
break;
default :
}
}
layout.show(this.mainPanel,cmd);
}
実行してみます。
今日の日付を指定してタスクを追加し、「今日」のボタンを押すと
しっかりとタスクが表示されている。
アプリ開発記の次の記事はこちら => アプリでタスクを一覧表示する(続き)- Javaでデスクトップトップアプリ(予定&ToDo管理)を作る-029