KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TableExample


1 /*
2  * @(#)TableExample.java 1.20 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)TableExample.java 1.20 05/11/17
39  */

40
41 /**
42  * A a UI around the JDBCAdaptor, allowing database data to be interactively
43  * fetched, sorted and displayed using Swing.
44  *
45  * NOTE: This example uses a modal dialog via the static convenience methods in
46  * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater.
47  *
48  * @version 1.20 11/17/05
49  * @author Philip Milne
50  */

51
52 import java.applet.Applet JavaDoc;
53 import java.awt.*;
54 import java.awt.event.*;
55 import javax.swing.*;
56 import javax.swing.table.*;
57 import javax.swing.event.*;
58 import javax.swing.border.*;
59
60 public class TableExample implements LayoutManager {
61     static String JavaDoc[] ConnectOptionNames = { "Connect" };
62     static String JavaDoc ConnectTitle = "Connection Information";
63
64     Dimension origin = new Dimension(0, 0);
65
66     JButton fetchButton;
67     JButton showConnectionInfoButton;
68
69     JPanel connectionPanel;
70     JFrame frame; // The query/results window.
71

72     JLabel userNameLabel;
73     JTextField userNameField;
74     JLabel passwordLabel;
75     JTextField passwordField;
76     // JLabel queryLabel;
77
JTextArea queryTextArea;
78     JComponent queryAggregate;
79     JLabel serverLabel;
80     JTextField serverField;
81     JLabel driverLabel;
82     JTextField driverField;
83
84     JPanel mainPanel;
85
86     TableSorter sorter;
87     JDBCAdapter dataBase;
88     JScrollPane tableAggregate;
89
90     /**
91      * Brigs up a JDialog using JOptionPane containing the connectionPanel.
92      * If the user clicks on the 'Connect' button the connection is reset.
93      */

94     void activateConnectionDialog() {
95     if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle,
96            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
97                    null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
98         connect();
99             frame.setVisible(true);
100     }
101     else if(!frame.isVisible())
102         System.exit(0);
103     }
104
105     /**
106      * Creates the connectionPanel, which will contain all the fields for
107      * the connection information.
108      */

109     public void createConnectionDialog() {
110     // Create the labels and text fields.
111
userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
112     userNameField = new JTextField("guest");
113
114     passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
115     passwordField = new JTextField("trustworthy");
116
117         serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
118     serverField = new JTextField("jdbc:sybase://dbtest:1455/pubs2");
119
120     driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
121     driverField = new JTextField("connect.sybase.SybaseDriver");
122
123
124     connectionPanel = new JPanel(false);
125     connectionPanel.setLayout(new BoxLayout(connectionPanel,
126                         BoxLayout.X_AXIS));
127
128     JPanel namePanel = new JPanel(false);
129     namePanel.setLayout(new GridLayout(0, 1));
130     namePanel.add(userNameLabel);
131     namePanel.add(passwordLabel);
132     namePanel.add(serverLabel);
133     namePanel.add(driverLabel);
134
135     JPanel fieldPanel = new JPanel(false);
136     fieldPanel.setLayout(new GridLayout(0, 1));
137     fieldPanel.add(userNameField);
138     fieldPanel.add(passwordField);
139     fieldPanel.add(serverField);
140         fieldPanel.add(driverField);
141
142     connectionPanel.add(namePanel);
143     connectionPanel.add(fieldPanel);
144     }
145
146     public TableExample() {
147         mainPanel = new JPanel();
148
149         // Create the panel for the connection information
150
createConnectionDialog();
151
152     // Create the buttons.
153
showConnectionInfoButton = new JButton("Configuration");
154         showConnectionInfoButton.addActionListener(new ActionListener() {
155             public void actionPerformed(ActionEvent e) {
156                 activateConnectionDialog();
157             }
158         }
159     );
160
161     fetchButton = new JButton("Fetch");
162         fetchButton.addActionListener(new ActionListener() {
163             public void actionPerformed(ActionEvent e) {
164                 fetch();
165             }
166         }
167     );
168
169     // Create the query text area and label.
170
queryTextArea = new JTextArea("SELECT * FROM titles", 25, 25);
171     queryAggregate = new JScrollPane(queryTextArea);
172         queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
173
174         // Create the table.
175
tableAggregate = createTable();
176         tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
177
178     // Add all the components to the main panel.
179
mainPanel.add(fetchButton);
180         mainPanel.add(showConnectionInfoButton);
181         mainPanel.add(queryAggregate);
182         mainPanel.add(tableAggregate);
183         mainPanel.setLayout(this);
184
185         // Create a Frame and put the main panel in it.
186
frame = new JFrame("TableExample");
187         frame.addWindowListener(new WindowAdapter() {
188             public void windowClosing(WindowEvent e) {System.exit(0);}});
189         frame.setBackground(Color.lightGray);
190         frame.getContentPane().add(mainPanel);
191         frame.pack();
192         frame.setVisible(false);
193         frame.setBounds(200, 200, 640, 480);
194
195     activateConnectionDialog();
196     }
197
198     public void connect() {
199        dataBase = new JDBCAdapter(
200             serverField.getText(),
201             driverField.getText(),
202             userNameField.getText(),
203             passwordField.getText());
204        sorter.setModel(dataBase);
205    }
206
207     public void fetch() {
208         dataBase.executeQuery(queryTextArea.getText());
209     }
210
211     public JScrollPane createTable() {
212         sorter = new TableSorter();
213
214         //connect();
215
//fetch();
216

217         // Create the table
218
JTable table = new JTable(sorter);
219     // Use a scrollbar, in case there are many columns.
220
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
221
222         // Install a mouse listener in the TableHeader as the sorter UI.
223
sorter.addMouseListenerToHeaderInTable(table);
224
225         JScrollPane scrollpane = new JScrollPane(table);
226
227         return scrollpane;
228     }
229
230     public static void main(String JavaDoc s[]) {
231         new TableExample();
232     }
233
234     public Dimension preferredLayoutSize(Container c){return origin;}
235     public Dimension minimumLayoutSize(Container c){return origin;}
236     public void addLayoutComponent(String JavaDoc s, Component c) {}
237     public void removeLayoutComponent(Component c) {}
238     public void layoutContainer(Container c) {
239         Rectangle b = c.getBounds();
240         int topHeight = 90;
241         int inset = 4;
242         showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25);
243         fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25);
244         // queryLabel.setBounds(10, 10, 100, 25);
245
queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80);
246         tableAggregate.setBounds(new Rectangle(inset,
247                                                inset + topHeight,
248                                                b.width-2*inset,
249                                                b.height-2*inset - topHeight));
250     }
251
252 }
253
Popular Tags