|
1 /* |
|
2 * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * |
|
8 * - Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * |
|
11 * - Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * |
|
15 * - Neither the name of Sun Microsystems nor the names of its |
|
16 * contributors may be used to endorse or promote products derived |
|
17 * from this software without specific prior written permission. |
|
18 * |
|
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 */ |
|
31 |
|
32 /* |
|
33 */ |
|
34 |
|
35 /** |
|
36 * A a UI around the JDBCAdaptor, allowing database data to be interactively |
|
37 * fetched, sorted and displayed using Swing. |
|
38 * |
|
39 * NOTE: This example uses a modal dialog via the static convenience methods in |
|
40 * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater. |
|
41 * |
|
42 * @author Philip Milne |
|
43 */ |
|
44 |
|
45 import java.applet.Applet; |
|
46 import java.awt.*; |
|
47 import java.awt.event.*; |
|
48 import javax.swing.*; |
|
49 import javax.swing.table.*; |
|
50 import javax.swing.event.*; |
|
51 import javax.swing.border.*; |
|
52 |
|
53 public class TableExample implements LayoutManager { |
|
54 static String[] ConnectOptionNames = { "Connect" }; |
|
55 static String ConnectTitle = "Connection Information"; |
|
56 |
|
57 Dimension origin = new Dimension(0, 0); |
|
58 |
|
59 JButton fetchButton; |
|
60 JButton showConnectionInfoButton; |
|
61 |
|
62 JPanel connectionPanel; |
|
63 JFrame frame; // The query/results window. |
|
64 |
|
65 JLabel userNameLabel; |
|
66 JTextField userNameField; |
|
67 JLabel passwordLabel; |
|
68 JTextField passwordField; |
|
69 // JLabel queryLabel; |
|
70 JTextArea queryTextArea; |
|
71 JComponent queryAggregate; |
|
72 JLabel serverLabel; |
|
73 JTextField serverField; |
|
74 JLabel driverLabel; |
|
75 JTextField driverField; |
|
76 |
|
77 JPanel mainPanel; |
|
78 |
|
79 TableSorter sorter; |
|
80 JDBCAdapter dataBase; |
|
81 JScrollPane tableAggregate; |
|
82 |
|
83 /** |
|
84 * Brigs up a JDialog using JOptionPane containing the connectionPanel. |
|
85 * If the user clicks on the 'Connect' button the connection is reset. |
|
86 */ |
|
87 void activateConnectionDialog() { |
|
88 if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle, |
|
89 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, |
|
90 null, ConnectOptionNames, ConnectOptionNames[0]) == 0) { |
|
91 connect(); |
|
92 frame.setVisible(true); |
|
93 } |
|
94 else if(!frame.isVisible()) |
|
95 System.exit(0); |
|
96 } |
|
97 |
|
98 /** |
|
99 * Creates the connectionPanel, which will contain all the fields for |
|
100 * the connection information. |
|
101 */ |
|
102 public void createConnectionDialog() { |
|
103 // Create the labels and text fields. |
|
104 userNameLabel = new JLabel("User name: ", JLabel.RIGHT); |
|
105 userNameField = new JTextField("guest"); |
|
106 |
|
107 passwordLabel = new JLabel("Password: ", JLabel.RIGHT); |
|
108 passwordField = new JTextField("trustworthy"); |
|
109 |
|
110 serverLabel = new JLabel("Database URL: ", JLabel.RIGHT); |
|
111 serverField = new JTextField("jdbc:sybase://dbtest:1455/pubs2"); |
|
112 |
|
113 driverLabel = new JLabel("Driver: ", JLabel.RIGHT); |
|
114 driverField = new JTextField("connect.sybase.SybaseDriver"); |
|
115 |
|
116 |
|
117 connectionPanel = new JPanel(false); |
|
118 connectionPanel.setLayout(new BoxLayout(connectionPanel, |
|
119 BoxLayout.X_AXIS)); |
|
120 |
|
121 JPanel namePanel = new JPanel(false); |
|
122 namePanel.setLayout(new GridLayout(0, 1)); |
|
123 namePanel.add(userNameLabel); |
|
124 namePanel.add(passwordLabel); |
|
125 namePanel.add(serverLabel); |
|
126 namePanel.add(driverLabel); |
|
127 |
|
128 JPanel fieldPanel = new JPanel(false); |
|
129 fieldPanel.setLayout(new GridLayout(0, 1)); |
|
130 fieldPanel.add(userNameField); |
|
131 fieldPanel.add(passwordField); |
|
132 fieldPanel.add(serverField); |
|
133 fieldPanel.add(driverField); |
|
134 |
|
135 connectionPanel.add(namePanel); |
|
136 connectionPanel.add(fieldPanel); |
|
137 } |
|
138 |
|
139 public TableExample() { |
|
140 mainPanel = new JPanel(); |
|
141 |
|
142 // Create the panel for the connection information |
|
143 createConnectionDialog(); |
|
144 |
|
145 // Create the buttons. |
|
146 showConnectionInfoButton = new JButton("Configuration"); |
|
147 showConnectionInfoButton.addActionListener(new ActionListener() { |
|
148 public void actionPerformed(ActionEvent e) { |
|
149 activateConnectionDialog(); |
|
150 } |
|
151 } |
|
152 ); |
|
153 |
|
154 fetchButton = new JButton("Fetch"); |
|
155 fetchButton.addActionListener(new ActionListener() { |
|
156 public void actionPerformed(ActionEvent e) { |
|
157 fetch(); |
|
158 } |
|
159 } |
|
160 ); |
|
161 |
|
162 // Create the query text area and label. |
|
163 queryTextArea = new JTextArea("SELECT * FROM titles", 25, 25); |
|
164 queryAggregate = new JScrollPane(queryTextArea); |
|
165 queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); |
|
166 |
|
167 // Create the table. |
|
168 tableAggregate = createTable(); |
|
169 tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); |
|
170 |
|
171 // Add all the components to the main panel. |
|
172 mainPanel.add(fetchButton); |
|
173 mainPanel.add(showConnectionInfoButton); |
|
174 mainPanel.add(queryAggregate); |
|
175 mainPanel.add(tableAggregate); |
|
176 mainPanel.setLayout(this); |
|
177 |
|
178 // Create a Frame and put the main panel in it. |
|
179 frame = new JFrame("TableExample"); |
|
180 frame.addWindowListener(new WindowAdapter() { |
|
181 public void windowClosing(WindowEvent e) {System.exit(0);}}); |
|
182 frame.setBackground(Color.lightGray); |
|
183 frame.getContentPane().add(mainPanel); |
|
184 frame.pack(); |
|
185 frame.setVisible(false); |
|
186 frame.setBounds(200, 200, 640, 480); |
|
187 |
|
188 activateConnectionDialog(); |
|
189 } |
|
190 |
|
191 public void connect() { |
|
192 dataBase = new JDBCAdapter( |
|
193 serverField.getText(), |
|
194 driverField.getText(), |
|
195 userNameField.getText(), |
|
196 passwordField.getText()); |
|
197 sorter.setModel(dataBase); |
|
198 } |
|
199 |
|
200 public void fetch() { |
|
201 dataBase.executeQuery(queryTextArea.getText()); |
|
202 } |
|
203 |
|
204 public JScrollPane createTable() { |
|
205 sorter = new TableSorter(); |
|
206 |
|
207 //connect(); |
|
208 //fetch(); |
|
209 |
|
210 // Create the table |
|
211 JTable table = new JTable(sorter); |
|
212 // Use a scrollbar, in case there are many columns. |
|
213 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
|
214 |
|
215 // Install a mouse listener in the TableHeader as the sorter UI. |
|
216 sorter.addMouseListenerToHeaderInTable(table); |
|
217 |
|
218 JScrollPane scrollpane = new JScrollPane(table); |
|
219 |
|
220 return scrollpane; |
|
221 } |
|
222 |
|
223 public static void main(String s[]) { |
|
224 new TableExample(); |
|
225 } |
|
226 |
|
227 public Dimension preferredLayoutSize(Container c){return origin;} |
|
228 public Dimension minimumLayoutSize(Container c){return origin;} |
|
229 public void addLayoutComponent(String s, Component c) {} |
|
230 public void removeLayoutComponent(Component c) {} |
|
231 public void layoutContainer(Container c) { |
|
232 Rectangle b = c.getBounds(); |
|
233 int topHeight = 90; |
|
234 int inset = 4; |
|
235 showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25); |
|
236 fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25); |
|
237 // queryLabel.setBounds(10, 10, 100, 25); |
|
238 queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80); |
|
239 tableAggregate.setBounds(new Rectangle(inset, |
|
240 inset + topHeight, |
|
241 b.width-2*inset, |
|
242 b.height-2*inset - topHeight)); |
|
243 } |
|
244 |
|
245 } |