src/demo/share/jfc/TableExample/TableExample.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/demo/share/jfc/TableExample/TableExample.java@3317bb8137f4
child 52252 de9486d74a74
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8979
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A a UI around the JDBCAdaptor, allowing database data to be interactively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * fetched, sorted and displayed using Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * NOTE: This example uses a modal dialog via the static convenience methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    51
import java.awt.Color;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
import java.awt.Component;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
import java.awt.Container;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
import java.awt.Dimension;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
import java.awt.GridLayout;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
import java.awt.LayoutManager;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
import java.awt.Rectangle;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
import java.awt.event.ActionEvent;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
import java.awt.event.ActionListener;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
import java.awt.event.WindowAdapter;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
import java.awt.event.WindowEvent;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
import java.util.logging.Level;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
import java.util.logging.Logger;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    64
import javax.swing.BoxLayout;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    65
import javax.swing.JButton;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    66
import javax.swing.JComponent;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
import javax.swing.JFrame;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
import javax.swing.JLabel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    69
import javax.swing.JOptionPane;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
import javax.swing.JPanel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
import javax.swing.JScrollPane;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
import javax.swing.JTable;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    73
import javax.swing.JTextArea;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    74
import javax.swing.JTextField;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    75
import javax.swing.UIManager;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    76
import javax.swing.UIManager.LookAndFeelInfo;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    77
import javax.swing.border.BevelBorder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    79
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    80
public final class TableExample implements LayoutManager {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    82
    static String[] ConnectOptionNames = { "Connect" };
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    83
    static String ConnectTitle = "Connection Information";
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    84
    Dimension origin = new Dimension(0, 0);
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    85
    JButton fetchButton;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    86
    JButton showConnectionInfoButton;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    87
    JPanel connectionPanel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    88
    JFrame frame; // The query/results window.
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    89
    JLabel userNameLabel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    90
    JTextField userNameField;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    91
    JLabel passwordLabel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    92
    JTextField passwordField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // JLabel      queryLabel;
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    94
    JTextArea queryTextArea;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    95
    JComponent queryAggregate;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    96
    JLabel serverLabel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    97
    JTextField serverField;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    98
    JLabel driverLabel;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
    99
    JTextField driverField;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   100
    JPanel mainPanel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    TableSorter sorter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    JDBCAdapter dataBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    JScrollPane tableAggregate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Brigs up a JDialog using JOptionPane containing the connectionPanel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * If the user clicks on the 'Connect' button the connection is reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    void activateConnectionDialog() {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
        if (JOptionPane.showOptionDialog(tableAggregate, connectionPanel,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
                ConnectTitle,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   113
                null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            frame.setVisible(true);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
        } else if (!frame.isVisible()) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   117
            System.exit(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates the connectionPanel, which will contain all the fields for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the connection information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void createConnectionDialog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // Create the labels and text fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   128
        userNameField = new JTextField("app");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   131
        passwordField = new JTextField("app");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   134
        serverField = new JTextField("jdbc:derby://localhost:1527/sample");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   137
        driverField = new JTextField("org.apache.derby.jdbc.ClientDriver");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        connectionPanel = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        connectionPanel.setLayout(new BoxLayout(connectionPanel,
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   142
                BoxLayout.X_AXIS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        JPanel namePanel = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        namePanel.setLayout(new GridLayout(0, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        namePanel.add(userNameLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        namePanel.add(passwordLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        namePanel.add(serverLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        namePanel.add(driverLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        JPanel fieldPanel = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        fieldPanel.setLayout(new GridLayout(0, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        fieldPanel.add(userNameField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        fieldPanel.add(passwordField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        fieldPanel.add(serverField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        fieldPanel.add(driverField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        connectionPanel.add(namePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        connectionPanel.add(fieldPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public TableExample() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        mainPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Create the panel for the connection information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        createConnectionDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // Create the buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        showConnectionInfoButton = new JButton("Configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        showConnectionInfoButton.addActionListener(new ActionListener() {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   171
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   172
            public void actionPerformed(ActionEvent e) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   173
                activateConnectionDialog();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   175
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        fetchButton = new JButton("Fetch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        fetchButton.addActionListener(new ActionListener() {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   179
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   180
            public void actionPerformed(ActionEvent e) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   181
                fetch();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   183
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Create the query text area and label.
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   186
        queryTextArea = new JTextArea("SELECT * FROM APP.CUSTOMER", 25, 25);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        queryAggregate = new JScrollPane(queryTextArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // Create the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        tableAggregate = createTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Add all the components to the main panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        mainPanel.add(fetchButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        mainPanel.add(showConnectionInfoButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        mainPanel.add(queryAggregate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        mainPanel.add(tableAggregate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        mainPanel.setLayout(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // Create a Frame and put the main panel in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        frame = new JFrame("TableExample");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        frame.addWindowListener(new WindowAdapter() {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   204
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   205
            @Override
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   206
            public void windowClosing(WindowEvent e) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   207
                System.exit(0);
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   208
            }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   209
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        frame.setBackground(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        frame.getContentPane().add(mainPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        frame.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        frame.setBounds(200, 200, 640, 480);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        activateConnectionDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void connect() {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   220
        dataBase = new JDBCAdapter(
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   221
                serverField.getText(),
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   222
                driverField.getText(),
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   223
                userNameField.getText(),
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   224
                passwordField.getText());
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   225
        sorter.setModel(dataBase);
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   226
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public void fetch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        dataBase.executeQuery(queryTextArea.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public JScrollPane createTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        sorter = new TableSorter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        //fetch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Create the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        JTable table = new JTable(sorter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // Use a scrollbar, in case there are many columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // Install a mouse listener in the TableHeader as the sorter UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        sorter.addMouseListenerToHeaderInTable(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        JScrollPane scrollpane = new JScrollPane(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return scrollpane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public static void main(String s[]) {
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   252
        // Trying to set Nimbus look and feel
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   253
        try {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   254
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   255
                if ("Nimbus".equals(info.getName())) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   256
                    UIManager.setLookAndFeel(info.getClassName());
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   257
                    break;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   258
                }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   259
            }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   260
        } catch (Exception ex) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   261
            Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   262
                    "Failed to apply Nimbus look and feel", ex);
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   263
        }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   264
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        new TableExample();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   268
    public Dimension preferredLayoutSize(Container c) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   269
        return origin;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   270
    }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   271
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   272
    public Dimension minimumLayoutSize(Container c) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   273
        return origin;
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   274
    }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   275
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   276
    public void addLayoutComponent(String s, Component c) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   277
    }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   278
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   279
    public void removeLayoutComponent(Component c) {
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   280
    }
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   281
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public void layoutContainer(Container c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Rectangle b = c.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int topHeight = 90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int inset = 4;
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   286
        showConnectionInfoButton.setBounds(b.width - 2 * inset - 120, inset, 120,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   287
                25);
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   288
        fetchButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // queryLabel.setBounds(10, 10, 100, 25);
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   290
        queryAggregate.setBounds(inset, inset, b.width - 2 * inset - 150, 80);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        tableAggregate.setBounds(new Rectangle(inset,
8979
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   292
                inset + topHeight,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   293
                b.width - 2 * inset,
30050c8aea19 7027701: /jfc/TableExample demo needs to be improved
mrkam
parents: 5506
diff changeset
   294
                b.height - 2 * inset - topHeight));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}