home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / TableExample / src / TableExample.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  8.4 KB  |  252 lines

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