KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > example > SearchUI


1 package net.walend.somnifugi.example;
2
3 import javax.swing.JFrame JavaDoc;
4 import javax.swing.JTextField JavaDoc;
5 import javax.swing.JTextArea JavaDoc;
6 import javax.swing.JLabel JavaDoc;
7 import javax.swing.JPanel JavaDoc;
8 import javax.swing.JButton JavaDoc;
9 import javax.swing.SwingUtilities JavaDoc;
10
11 import java.awt.FlowLayout JavaDoc;
12 import java.awt.BorderLayout JavaDoc;
13 import java.awt.Dimension JavaDoc;
14
15 import java.awt.event.ActionListener JavaDoc;
16 import java.awt.event.ActionEvent JavaDoc;
17
18 /**
19 SearchUI shows a common UI for both examples.
20 */

21
22 public abstract class SearchUI
23     implements LookupListener
24 {
25     protected JTextField JavaDoc searchTF;
26     private JTextArea JavaDoc outputTA;
27     private JFrame JavaDoc frame;
28
29     public SearchUI()
30     {
31         layoutComponents();
32     }
33
34     private void layoutComponents()
35     {
36         frame = new JFrame JavaDoc("CommandQueueExample");
37         searchTF = new JTextField JavaDoc();
38         outputTA = new JTextArea JavaDoc();
39         outputTA.setEditable(false);
40
41         JLabel JavaDoc searchLabel = new JLabel JavaDoc("Search");
42         JPanel JavaDoc topPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.LEFT));
43         JButton JavaDoc searchButton = new JButton JavaDoc("Go");
44         
45         searchButton.addActionListener(new ActionListener JavaDoc()
46             {
47                 public void actionPerformed(ActionEvent JavaDoc e)
48                 {
49                     searchButtonActionPerformed();
50                 }
51             });
52         
53         frame.getContentPane().setLayout(new BorderLayout JavaDoc());
54         
55         searchLabel.setPreferredSize(new Dimension JavaDoc(45, 26));
56         topPanel.add(searchLabel);
57         searchTF.setPreferredSize(new Dimension JavaDoc(100, 26));
58         topPanel.add(searchTF);
59         topPanel.add(searchButton);
60         
61         frame.getContentPane().add(topPanel, BorderLayout.NORTH);
62         frame.getContentPane().add(outputTA, BorderLayout.CENTER);
63         
64         frame.setLocation(200,200);
65         frame.setSize(300,300);
66
67         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68     }
69
70     void showFrame()
71     {
72         SwingUtilities.invokeLater(new Runnable JavaDoc()
73             {
74                 public void run()
75                 {
76                     frame.setVisible(true);
77                 }
78             });
79     }
80
81     protected void handleThrowable(Throwable JavaDoc thrown)
82     {
83         thrown.printStackTrace();
84     }
85
86     /**
87 Called from the UI thread when someone pushes the search button.
88     */

89     protected abstract void searchButtonActionPerformed() ;
90     
91     public void lookupStarted(final LookupRequest request)
92     {
93         SwingUtilities.invokeLater(new Runnable JavaDoc()
94             {
95                 public void run()
96                 {
97                     outputTA.setText("Searching for: " + request.getSearchText());
98                 }
99             });
100     }
101
102     public void lookupCompleted(final LookupResults result)
103     {
104         SwingUtilities.invokeLater(new Runnable JavaDoc()
105             {
106                 public void run()
107                 {
108                     outputTA.setText("");
109                     String JavaDoc[] results = result.getResults();
110                     for (int i = 0; i < results.length; i++)
111                     {
112                         String JavaDoc result = results[i];
113                         outputTA.setText(outputTA.getText() + "\n" + result);
114                     }
115                 }
116             });
117     }
118 }
119
Popular Tags