��5��ł́AToDo���X�g�̕\����ToDo�̒lj����������܂����B��6��ł͂���Ȃ�@�\�̎����ƑO��G��Ă��Ȃ��_�ɂ��ĉ�����s���܂��B
�@����͑�5��ō쐬�����A�v���P�[�V�����ɑ��Ĉȉ��̋@�\���������܂��B
�@����́uToDoList2�v�Ƃ������O�̃v���W�F�N�g���쐬���܂��i��1��́g�v���W�F�N�g�̍쐬�h���Q�l�ɂ��Ă��������j�B
�@�A�v���P�[�V�����̋N������ѓy��ƂȂ�t���[���̐�����S������N���X�uSwingAppMain�v���쐬���܂��B
package swing.sample; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class SwingAppMain { /** * �A�v���P�[�V�����̋N�� */ public static void main(String[] args) { System.out.println("main : " + SwingUtilities.isEventDispatchThread()); SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowTodoList(); } }); } /** * ToDo���X�g�̐����ƕ\�����s���܂��B */ private static void createAndShowTodoList() { System.out.println("createAndShowTodoList :" + SwingUtilities. isEventDispatchThread()); JFrame mainFrame = new JFrame("ToDo���X�g"); mainFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Container contentPane = mainFrame.getContentPane(); // ToDo���X�g�� JComponent newContentPane = new ToDoListPane(); contentPane.add(newContentPane, BorderLayout.CENTER); // Window�T�C�Y�� mainFrame.pack(); // �\�� mainFrame.setVisible(true); } }
�@����ToDoListPane�N���X���쐬���܂��B���̃N���X�́AToDo���X�g�̂��ׂĂ̋@�\�����N���X�ł��B
package swing.sample; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; /** * ToDo���X�g */ public class ToDoListPane extends JPanel { private JList toDoList; private DefaultListModel toDoListModel; private JTextField toDoInputField; private JButton addButton; private JButton modifyButton; private JButton removeButton; public ToDoListPane() { super(new BorderLayout()); // �ꗗ�� toDoListModel = new DefaultListModel(); toDoList = new JList(toDoListModel); JScrollPane listScrollPane = new JScrollPane(toDoList); // TODO���X�g�Ƀ��X�i��ݒ� toDoList.addListSelectionListener (new ToDoListSelectionHandler()); // ToDo�lj��p�e�L�X�g�t�B�[���h�̐��� toDoInputField = new JTextField(); // �e�{�^���̐��� JPanel buttonPanel = new JPanel(); addButton = new JButton("�lj�"); modifyButton = new JButton("�ҏW"); removeButton = new JButton("�폜"); // �{�^���Ƀ��X�i��ݒ� addButton.addActionListener (new AddActionHandler()); modifyButton.addActionListener (new ModifyActionHandler()); removeButton.addActionListener (new RemoveActionHandler()); buttonPanel.add(addButton); buttonPanel.add(modifyButton); buttonPanel.add(removeButton); add(listScrollPane, BorderLayout.NORTH); add(toDoInputField, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); } /** * TODO���X�g�I���A�N�V�����̃n���h�� */ private class ToDoListSelectionHandler implements ListSelectionListener { public void valueChanged(ListSelectionEvent arg0) { // TODO���X�g�̂ǂ̍s���I������� ���Ȃ��ꍇ��A // �����s���I������Ă���ꍇ�́A // �������܂��� if (toDoList.getSelectedIndices(). length!=1){ return; } toDoInputField.setText((String)toDoList. getSelectedValue()); } } /** * �lj��{�^���A�N�V�����̃n���h�� */ private class AddActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // �e�L�X�g�t�B�[���h�̓��e�� ���X�g���f���ɒlj� toDoListModel.addElement (toDoInputField.getText()); } } /** * �ҏW�{�^���A�N�V�����̃n���h�� */ private class ModifyActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // �e�L�X�g�t�B�[���h�̓��e�� ���X�g���f����ҏW // TODO���X�g�̂ǂ̍s�� �I������Ă��Ȃ��ꍇ��A // �����s���I������Ă���ꍇ�́A // �������܂��� if (toDoList.getSelectedIndices(). length != 1) { return; } toDoListModel.set(toDoList. getSelectedIndex(), toDoInputField.getText()); } } /** * �폜�{�^���A�N�V�����̃n���h�� */ private class RemoveActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // �e�L�X�g�t�B�[���h�̓��e�� ���X�g���f���ɒlj� // TODO���X�g�̂ǂ̍s�� �I������Ă��Ȃ��ꍇ��A // �����s���I������Ă���ꍇ�́A // �������܂��� if (toDoList.getSelectedIndices(). length != 1) { return; } setButtonsEnabled(false); Thread removeThread = new RemoveThread(toDoList.getSelectedIndex()); removeThread.start(); } } /** * �{�^���̏�Ԃ��X�V���܂� */ private void setButtonsEnabled(boolean enabled) { addButton.setEnabled(enabled); modifyButton.setEnabled(enabled); removeButton.setEnabled(enabled); } /** * �폜�������s���N���X */ class RemoveThread extends Thread { int index; RemoveThread(int index) { this.index = index; } public void run() { // ���Ԃ̂����鏈�������s doLongTask(); // �C�x���g�f�B�X�p�b�`�X���b�h�� �{�^����ύX SwingUtilities.invokeLater(new Runnable() { public void run() { toDoListModel. remove(index); setButtonsEnabled(true); } }); } } /** *�@���Ԃ̂����鏈�� */ private void doLongTask() { try { Thread.sleep(10000); } catch (InterruptedException ex) { } }
�@�uSwingAppMain�v�N���X�����s���AToDo��lj����Ă݂܂��i�}2�ł́u20:00�ގЁv�̒lj������݂悤�Ƃ��Ă���j�B
�@�Ȃ��A����͈ȉ��̂悤�ȋ@�\���������܂����B
(1)�m�ҏW�n�{�^�����N���b�N����ƁA�ꗗ�̑I������Ă���1�s�̕����e�L�X�g�t�B�[���h�ɕ\������Ă��镶����ɕύX�����
(2)�m�폜�n�{�^�����N���b�N����ƁA�ꗗ�̑I������Ă���1�s���폜�����
Copyright © ITmedia, Inc. All Rights Reserved.