1 /* 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. 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 Oracle 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 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 42 /** 43 * A a UI around the JDBCAdaptor, allowing database data to be interactively 44 * fetched, sorted and displayed using Swing. 45 * 46 * NOTE: This example uses a modal dialog via the static convenience methods in 47 * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater. 48 * 49 * @author Philip Milne 50 */ 51 import java.awt.Color; 52 import java.awt.Component; 53 import java.awt.Container; 54 import java.awt.Dimension; 55 import java.awt.GridLayout; 56 import java.awt.LayoutManager; 57 import java.awt.Rectangle; 58 import java.awt.event.ActionEvent; 59 import java.awt.event.ActionListener; 60 import java.awt.event.WindowAdapter; 61 import java.awt.event.WindowEvent; 62 import java.util.logging.Level; 63 import java.util.logging.Logger; 64 import javax.swing.BoxLayout; 65 import javax.swing.JButton; 66 import javax.swing.JComponent; 67 import javax.swing.JFrame; 68 import javax.swing.JLabel; 69 import javax.swing.JOptionPane; 70 import javax.swing.JPanel; 71 import javax.swing.JScrollPane; 72 import javax.swing.JTable; 73 import javax.swing.JTextArea; 74 import javax.swing.JTextField; 75 import javax.swing.UIManager; 76 import javax.swing.UIManager.LookAndFeelInfo; 77 import javax.swing.border.BevelBorder; 78 79 80 public final class TableExample implements LayoutManager { 81 82 static String[] ConnectOptionNames = { "Connect" }; 83 static String ConnectTitle = "Connection Information"; 84 Dimension origin = new Dimension(0, 0); 85 JButton fetchButton; 86 JButton showConnectionInfoButton; 87 JPanel connectionPanel; 88 JFrame frame; // The query/results window. 89 JLabel userNameLabel; 90 JTextField userNameField; 91 JLabel passwordLabel; 92 JTextField passwordField; 93 // JLabel queryLabel; 94 JTextArea queryTextArea; 95 JComponent queryAggregate; 96 JLabel serverLabel; 97 JTextField serverField; 98 JLabel driverLabel; 99 JTextField driverField; 100 JPanel mainPanel; 101 TableSorter sorter; 102 JDBCAdapter dataBase; 103 JScrollPane tableAggregate; 104 105 /** 106 * Brigs up a JDialog using JOptionPane containing the connectionPanel. 107 * If the user clicks on the 'Connect' button the connection is reset. 108 */ 109 void activateConnectionDialog() { 110 if (JOptionPane.showOptionDialog(tableAggregate, connectionPanel, 111 ConnectTitle, 112 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, 113 null, ConnectOptionNames, ConnectOptionNames[0]) == 0) { 114 connect(); 115 frame.setVisible(true); 116 } else if (!frame.isVisible()) { 117 System.exit(0); 118 } 119 } 120 121 /** 122 * Creates the connectionPanel, which will contain all the fields for 123 * the connection information. 124 */ 125 public void createConnectionDialog() { 126 // Create the labels and text fields. 127 userNameLabel = new JLabel("User name: ", JLabel.RIGHT); 128 userNameField = new JTextField("app"); 129 130 passwordLabel = new JLabel("Password: ", JLabel.RIGHT); 131 passwordField = new JTextField("app"); 132 133 serverLabel = new JLabel("Database URL: ", JLabel.RIGHT); 134 serverField = new JTextField("jdbc:derby://localhost:1527/sample"); 135 136 driverLabel = new JLabel("Driver: ", JLabel.RIGHT); 137 driverField = new JTextField("org.apache.derby.jdbc.ClientDriver"); 138 139 140 connectionPanel = new JPanel(false); 141 connectionPanel.setLayout(new BoxLayout(connectionPanel, 142 BoxLayout.X_AXIS)); 143 144 JPanel namePanel = new JPanel(false); 145 namePanel.setLayout(new GridLayout(0, 1)); 146 namePanel.add(userNameLabel); 147 namePanel.add(passwordLabel); 148 namePanel.add(serverLabel); 149 namePanel.add(driverLabel); 150 151 JPanel fieldPanel = new JPanel(false); 152 fieldPanel.setLayout(new GridLayout(0, 1)); 153 fieldPanel.add(userNameField); 154 fieldPanel.add(passwordField); 155 fieldPanel.add(serverField); 156 fieldPanel.add(driverField); 157 158 connectionPanel.add(namePanel); 159 connectionPanel.add(fieldPanel); 160 } 161 162 public TableExample() { 163 mainPanel = new JPanel(); 164 165 // Create the panel for the connection information 166 createConnectionDialog(); 167 168 // Create the buttons. 169 showConnectionInfoButton = new JButton("Configuration"); 170 showConnectionInfoButton.addActionListener(new ActionListener() { 171 172 public void actionPerformed(ActionEvent e) { 173 activateConnectionDialog(); 174 } 175 }); 176 177 fetchButton = new JButton("Fetch"); 178 fetchButton.addActionListener(new ActionListener() { 179 180 public void actionPerformed(ActionEvent e) { 181 fetch(); 182 } 183 }); 184 185 // Create the query text area and label. 186 queryTextArea = new JTextArea("SELECT * FROM APP.CUSTOMER", 25, 25); 187 queryAggregate = new JScrollPane(queryTextArea); 188 queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); 189 190 // Create the table. 191 tableAggregate = createTable(); 192 tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); 193 194 // Add all the components to the main panel. 195 mainPanel.add(fetchButton); 196 mainPanel.add(showConnectionInfoButton); 197 mainPanel.add(queryAggregate); 198 mainPanel.add(tableAggregate); 199 mainPanel.setLayout(this); 200 201 // Create a Frame and put the main panel in it. 202 frame = new JFrame("TableExample"); 203 frame.addWindowListener(new WindowAdapter() { 204 205 @Override 206 public void windowClosing(WindowEvent e) { 207 System.exit(0); 208 } 209 }); 210 frame.setBackground(Color.lightGray); 211 frame.getContentPane().add(mainPanel); 212 frame.pack(); 213 frame.setVisible(false); 214 frame.setBounds(200, 200, 640, 480); 215 216 activateConnectionDialog(); 217 } 218 219 public void connect() { 220 dataBase = new JDBCAdapter( 221 serverField.getText(), 222 driverField.getText(), 223 userNameField.getText(), 224 passwordField.getText()); 225 sorter.setModel(dataBase); 226 } 227 228 public void fetch() { 229 dataBase.executeQuery(queryTextArea.getText()); 230 } 231 232 public JScrollPane createTable() { 233 sorter = new TableSorter(); 234 235 //connect(); 236 //fetch(); 237 238 // Create the table 239 JTable table = new JTable(sorter); 240 // Use a scrollbar, in case there are many columns. 241 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 242 243 // Install a mouse listener in the TableHeader as the sorter UI. 244 sorter.addMouseListenerToHeaderInTable(table); 245 246 JScrollPane scrollpane = new JScrollPane(table); 247 248 return scrollpane; 249 } 250 251 public static void main(String[] s) { 252 // Trying to set Nimbus look and feel 253 try { 254 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 255 if ("Nimbus".equals(info.getName())) { 256 UIManager.setLookAndFeel(info.getClassName()); 257 break; 258 } 259 } 260 } catch (Exception ex) { 261 Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE, 262 "Failed to apply Nimbus look and feel", ex); 263 } 264 265 new TableExample(); 266 } 267 268 public Dimension preferredLayoutSize(Container c) { 269 return origin; 270 } 271 272 public Dimension minimumLayoutSize(Container c) { 273 return origin; 274 } 275 276 public void addLayoutComponent(String s, Component c) { 277 } 278 279 public void removeLayoutComponent(Component c) { 280 } 281 282 public void layoutContainer(Container c) { 283 Rectangle b = c.getBounds(); 284 int topHeight = 90; 285 int inset = 4; 286 showConnectionInfoButton.setBounds(b.width - 2 * inset - 120, inset, 120, 287 25); 288 fetchButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25); 289 // queryLabel.setBounds(10, 10, 100, 25); 290 queryAggregate.setBounds(inset, inset, b.width - 2 * inset - 150, 80); 291 tableAggregate.setBounds(new Rectangle(inset, 292 inset + topHeight, 293 b.width - 2 * inset, 294 b.height - 2 * inset - topHeight)); 295 } 296 }