KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > example > layering > gui > ThingerUI


1 package net.innig.macker.example.layering.gui;
2
3 import net.innig.macker.example.layering.model.*;
4 import net.innig.macker.example.layering.persistence.*;
5
6 import javax.swing.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.util.*;
10
11 public class ThingerUI
12     {
13     public static void main(String JavaDoc[] args)
14         throws PersistenceException
15         { new ThingerUI(); }
16     
17     public ThingerUI()
18         throws PersistenceException
19         {
20         JFrame f = new JFrame();
21         JPanel panel = new JPanel(new FlowLayout());
22         f.getContentPane().add(panel);
23         
24         list = new JList();
25         list.setPreferredSize(new Dimension(300, 400));
26         refresh();
27         panel.add(list);
28         
29         JButton refresh = new JButton("Refresh");
30         refresh.addActionListener(
31             new ActionListener()
32                 {
33                 public void actionPerformed(ActionEvent e)
34                     { refresh(); }
35                 });
36         panel.add(refresh);
37         
38         newName = new JTextField();
39         newName.setPreferredSize(new Dimension(150, 24));
40         panel.add(newName);
41         
42         JButton add = new JButton("Add Thinger");
43         add.addActionListener(
44             new ActionListener()
45                 {
46                 public void actionPerformed(ActionEvent e)
47                     { add(newName.getText()); }
48                 });
49         panel.add(add);
50         
51         f.pack();
52         f.show();
53         }
54     
55     public void refresh()
56         {
57         try { list.setListData(Thinger.getAll().toArray()); }
58         catch(PersistenceException pe)
59             { pe.printStackTrace(); }
60         }
61     
62     public void add(String JavaDoc name)
63         {
64         try
65             {
66             new Thinger(name).store();
67             refresh();
68             }
69         catch(PersistenceException pe)
70             { pe.printStackTrace(); }
71         }
72     
73     private JTextField newName;
74     private JList list;
75     }
Popular Tags