0N/A/*
3853N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A
0N/A/**
0N/A * A a UI around the JDBCAdaptor, allowing database data to be interactively
0N/A * fetched, sorted and displayed using Swing.
0N/A *
0N/A * NOTE: This example uses a modal dialog via the static convenience methods in
0N/A * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater.
0N/A *
0N/A * @author Philip Milne
0N/A */
3853N/Aimport java.awt.Color;
3853N/Aimport java.awt.Component;
3853N/Aimport java.awt.Container;
3853N/Aimport java.awt.Dimension;
3853N/Aimport java.awt.GridLayout;
3853N/Aimport java.awt.LayoutManager;
3853N/Aimport java.awt.Rectangle;
3853N/Aimport java.awt.event.ActionEvent;
3853N/Aimport java.awt.event.ActionListener;
3853N/Aimport java.awt.event.WindowAdapter;
3853N/Aimport java.awt.event.WindowEvent;
3853N/Aimport java.util.logging.Level;
3853N/Aimport java.util.logging.Logger;
3853N/Aimport javax.swing.BoxLayout;
3853N/Aimport javax.swing.JButton;
3853N/Aimport javax.swing.JComponent;
3853N/Aimport javax.swing.JFrame;
3853N/Aimport javax.swing.JLabel;
3853N/Aimport javax.swing.JOptionPane;
3853N/Aimport javax.swing.JPanel;
3853N/Aimport javax.swing.JScrollPane;
3853N/Aimport javax.swing.JTable;
3853N/Aimport javax.swing.JTextArea;
3853N/Aimport javax.swing.JTextField;
3853N/Aimport javax.swing.UIManager;
3853N/Aimport javax.swing.UIManager.LookAndFeelInfo;
3853N/Aimport javax.swing.border.BevelBorder;
0N/A
3853N/A
3853N/Apublic final class TableExample implements LayoutManager {
0N/A
3853N/A static String[] ConnectOptionNames = { "Connect" };
3853N/A static String ConnectTitle = "Connection Information";
3853N/A Dimension origin = new Dimension(0, 0);
3853N/A JButton fetchButton;
3853N/A JButton showConnectionInfoButton;
3853N/A JPanel connectionPanel;
3853N/A JFrame frame; // The query/results window.
3853N/A JLabel userNameLabel;
3853N/A JTextField userNameField;
3853N/A JLabel passwordLabel;
3853N/A JTextField passwordField;
0N/A // JLabel queryLabel;
3853N/A JTextArea queryTextArea;
3853N/A JComponent queryAggregate;
3853N/A JLabel serverLabel;
3853N/A JTextField serverField;
3853N/A JLabel driverLabel;
3853N/A JTextField driverField;
3853N/A JPanel mainPanel;
0N/A TableSorter sorter;
0N/A JDBCAdapter dataBase;
0N/A JScrollPane tableAggregate;
0N/A
0N/A /**
0N/A * Brigs up a JDialog using JOptionPane containing the connectionPanel.
0N/A * If the user clicks on the 'Connect' button the connection is reset.
0N/A */
0N/A void activateConnectionDialog() {
3853N/A if (JOptionPane.showOptionDialog(tableAggregate, connectionPanel,
3853N/A ConnectTitle,
3853N/A JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
3853N/A null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
0N/A connect();
0N/A frame.setVisible(true);
3853N/A } else if (!frame.isVisible()) {
3853N/A System.exit(0);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the connectionPanel, which will contain all the fields for
0N/A * the connection information.
0N/A */
0N/A public void createConnectionDialog() {
0N/A // Create the labels and text fields.
0N/A userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
3853N/A userNameField = new JTextField("app");
0N/A
0N/A passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
3853N/A passwordField = new JTextField("app");
0N/A
0N/A serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
3853N/A serverField = new JTextField("jdbc:derby://localhost:1527/sample");
0N/A
0N/A driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
3853N/A driverField = new JTextField("org.apache.derby.jdbc.ClientDriver");
0N/A
0N/A
0N/A connectionPanel = new JPanel(false);
0N/A connectionPanel.setLayout(new BoxLayout(connectionPanel,
3853N/A BoxLayout.X_AXIS));
0N/A
0N/A JPanel namePanel = new JPanel(false);
0N/A namePanel.setLayout(new GridLayout(0, 1));
0N/A namePanel.add(userNameLabel);
0N/A namePanel.add(passwordLabel);
0N/A namePanel.add(serverLabel);
0N/A namePanel.add(driverLabel);
0N/A
0N/A JPanel fieldPanel = new JPanel(false);
0N/A fieldPanel.setLayout(new GridLayout(0, 1));
0N/A fieldPanel.add(userNameField);
0N/A fieldPanel.add(passwordField);
0N/A fieldPanel.add(serverField);
0N/A fieldPanel.add(driverField);
0N/A
0N/A connectionPanel.add(namePanel);
0N/A connectionPanel.add(fieldPanel);
0N/A }
0N/A
0N/A public TableExample() {
0N/A mainPanel = new JPanel();
0N/A
0N/A // Create the panel for the connection information
0N/A createConnectionDialog();
0N/A
0N/A // Create the buttons.
0N/A showConnectionInfoButton = new JButton("Configuration");
0N/A showConnectionInfoButton.addActionListener(new ActionListener() {
3853N/A
3853N/A public void actionPerformed(ActionEvent e) {
3853N/A activateConnectionDialog();
0N/A }
3853N/A });
0N/A
0N/A fetchButton = new JButton("Fetch");
0N/A fetchButton.addActionListener(new ActionListener() {
3853N/A
3853N/A public void actionPerformed(ActionEvent e) {
3853N/A fetch();
0N/A }
3853N/A });
0N/A
0N/A // Create the query text area and label.
3853N/A queryTextArea = new JTextArea("SELECT * FROM APP.CUSTOMER", 25, 25);
0N/A queryAggregate = new JScrollPane(queryTextArea);
0N/A queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
0N/A
0N/A // Create the table.
0N/A tableAggregate = createTable();
0N/A tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
0N/A
0N/A // Add all the components to the main panel.
0N/A mainPanel.add(fetchButton);
0N/A mainPanel.add(showConnectionInfoButton);
0N/A mainPanel.add(queryAggregate);
0N/A mainPanel.add(tableAggregate);
0N/A mainPanel.setLayout(this);
0N/A
0N/A // Create a Frame and put the main panel in it.
0N/A frame = new JFrame("TableExample");
0N/A frame.addWindowListener(new WindowAdapter() {
3853N/A
3853N/A @Override
3853N/A public void windowClosing(WindowEvent e) {
3853N/A System.exit(0);
3853N/A }
3853N/A });
0N/A frame.setBackground(Color.lightGray);
0N/A frame.getContentPane().add(mainPanel);
0N/A frame.pack();
0N/A frame.setVisible(false);
0N/A frame.setBounds(200, 200, 640, 480);
0N/A
0N/A activateConnectionDialog();
0N/A }
0N/A
0N/A public void connect() {
3853N/A dataBase = new JDBCAdapter(
3853N/A serverField.getText(),
3853N/A driverField.getText(),
3853N/A userNameField.getText(),
3853N/A passwordField.getText());
3853N/A sorter.setModel(dataBase);
3853N/A }
0N/A
0N/A public void fetch() {
0N/A dataBase.executeQuery(queryTextArea.getText());
0N/A }
0N/A
0N/A public JScrollPane createTable() {
0N/A sorter = new TableSorter();
0N/A
0N/A //connect();
0N/A //fetch();
0N/A
0N/A // Create the table
0N/A JTable table = new JTable(sorter);
0N/A // Use a scrollbar, in case there are many columns.
0N/A table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
0N/A
0N/A // Install a mouse listener in the TableHeader as the sorter UI.
0N/A sorter.addMouseListenerToHeaderInTable(table);
0N/A
0N/A JScrollPane scrollpane = new JScrollPane(table);
0N/A
0N/A return scrollpane;
0N/A }
0N/A
0N/A public static void main(String s[]) {
3853N/A // Trying to set Nimbus look and feel
3853N/A try {
3853N/A for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
3853N/A if ("Nimbus".equals(info.getName())) {
3853N/A UIManager.setLookAndFeel(info.getClassName());
3853N/A break;
3853N/A }
3853N/A }
3853N/A } catch (Exception ex) {
3853N/A Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE,
3853N/A "Failed to apply Nimbus look and feel", ex);
3853N/A }
3853N/A
0N/A new TableExample();
0N/A }
0N/A
3853N/A public Dimension preferredLayoutSize(Container c) {
3853N/A return origin;
3853N/A }
3853N/A
3853N/A public Dimension minimumLayoutSize(Container c) {
3853N/A return origin;
3853N/A }
3853N/A
3853N/A public void addLayoutComponent(String s, Component c) {
3853N/A }
3853N/A
3853N/A public void removeLayoutComponent(Component c) {
3853N/A }
3853N/A
0N/A public void layoutContainer(Container c) {
0N/A Rectangle b = c.getBounds();
0N/A int topHeight = 90;
0N/A int inset = 4;
3853N/A showConnectionInfoButton.setBounds(b.width - 2 * inset - 120, inset, 120,
3853N/A 25);
3853N/A fetchButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25);
0N/A // queryLabel.setBounds(10, 10, 100, 25);
3853N/A queryAggregate.setBounds(inset, inset, b.width - 2 * inset - 150, 80);
0N/A tableAggregate.setBounds(new Rectangle(inset,
3853N/A inset + topHeight,
3853N/A b.width - 2 * inset,
3853N/A b.height - 2 * inset - topHeight));
0N/A }
0N/A}