home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 July / CHIP-1999-07.iso / software / jdk / jdk121.exe / disk1 / data1.cab / demos / demo / jfc / Table / TableExample2.java < prev    next >
Encoding:
Java Source  |  1999-03-27  |  7.0 KB  |  227 lines

  1. /*
  2.  * @(#)TableExample2.java    1.11 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /**
  16.  * A a UI around the JDBCAdaptor, allowing database data to be interactively
  17.  * fetched, sorted and displayed using Swing.
  18.  *
  19.  * NOTE: This example uses a modal dialog via the static convenience methods in
  20.  * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater.
  21.  *
  22.  * @version 1.11 08/26/98
  23.  * @author Philip Milne
  24.  */
  25.  
  26. import java.applet.Applet;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import javax.swing.*;
  30. import javax.swing.table.*;
  31. import javax.swing.event.*;
  32. import javax.swing.border.*;
  33.  
  34. public class TableExample2 implements LayoutManager {
  35.     static String[] ConnectOptionNames = { "Connect" };
  36.     static String   ConnectTitle = "Connection Information";
  37.  
  38.     Dimension   origin = new Dimension(0, 0);
  39.  
  40.     JButton     fetchButton;
  41.     JButton     showConnectionInfoButton;
  42.  
  43.     JPanel      connectionPanel;
  44.     JFrame      frame; // The query/results window.
  45.  
  46.     JLabel      userNameLabel;
  47.     JTextField  userNameField;
  48.     JLabel      passwordLabel;
  49.     JTextField  passwordField;
  50.     // JLabel      queryLabel;
  51.     JTextArea   queryTextArea;
  52.     JComponent  queryAggregate;
  53.     JLabel      serverLabel;
  54.     JTextField  serverField;
  55.     JLabel      driverLabel;
  56.     JTextField  driverField;
  57.  
  58.     JPanel      mainPanel;
  59.  
  60.     TableSorter sorter;
  61.     JDBCAdapter dataBase;
  62.     JScrollPane tableAggregate;
  63.  
  64.     /**
  65.      * Brigs up a JDialog using JOptionPane containing the connectionPanel.
  66.      * If the user clicks on the 'Connect' button the connection is reset.
  67.      */
  68.     void activateConnectionDialog() {
  69.     if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle,
  70.            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
  71.                    null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
  72.         connect();
  73.             frame.setVisible(true);
  74.     }
  75.     else if(!frame.isVisible())
  76.         System.exit(0);
  77.     }
  78.  
  79.     /**
  80.      * Creates the connectionPanel, which will contain all the fields for
  81.      * the connection information.
  82.      */
  83.     public void createConnectionDialog() {
  84.      // Create the labels and text fields.
  85.     userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
  86.      userNameField = new JTextField("guest");
  87.  
  88.     passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
  89.     passwordField = new JTextField("trustworthy");
  90.  
  91.         serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
  92.     serverField = new JTextField("jdbc:sybase://dbtest:1455/pubs2");
  93.  
  94.     driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
  95.     driverField = new JTextField("connect.sybase.SybaseDriver");
  96.  
  97.  
  98.     connectionPanel = new JPanel(false);
  99.     connectionPanel.setLayout(new BoxLayout(connectionPanel,
  100.                         BoxLayout.X_AXIS));
  101.  
  102.     JPanel namePanel = new JPanel(false);
  103.     namePanel.setLayout(new GridLayout(0, 1));
  104.     namePanel.add(userNameLabel);
  105.     namePanel.add(passwordLabel);
  106.     namePanel.add(serverLabel);
  107.     namePanel.add(driverLabel);
  108.  
  109.     JPanel fieldPanel = new JPanel(false);
  110.     fieldPanel.setLayout(new GridLayout(0, 1));
  111.     fieldPanel.add(userNameField);
  112.     fieldPanel.add(passwordField);
  113.     fieldPanel.add(serverField);
  114.         fieldPanel.add(driverField);
  115.  
  116.     connectionPanel.add(namePanel);
  117.     connectionPanel.add(fieldPanel);
  118.     }
  119.  
  120.     public TableExample2() {
  121.         mainPanel = new JPanel();
  122.  
  123.         // Create the panel for the connection information
  124.     createConnectionDialog();
  125.  
  126.     // Create the buttons.
  127.     showConnectionInfoButton = new JButton("Configuration");
  128.         showConnectionInfoButton.addActionListener(new ActionListener() {
  129.             public void actionPerformed(ActionEvent e) {
  130.                 activateConnectionDialog();
  131.             }
  132.         }
  133.     );
  134.  
  135.     fetchButton = new JButton("Fetch");
  136.         fetchButton.addActionListener(new ActionListener() {
  137.             public void actionPerformed(ActionEvent e) {
  138.                 fetch();
  139.             }
  140.         }
  141.     );
  142.  
  143.     // Create the query text area and label.
  144.         queryTextArea = new JTextArea("SELECT * FROM titles", 25, 25);
  145.     queryAggregate = new JScrollPane(queryTextArea);
  146.         queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
  147.  
  148.         // Create the table.
  149.         tableAggregate = createTable();
  150.         tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));
  151.  
  152.     // Add all the components to the main panel.
  153.         mainPanel.add(fetchButton);
  154.         mainPanel.add(showConnectionInfoButton);
  155.         mainPanel.add(queryAggregate);
  156.         mainPanel.add(tableAggregate);
  157.         mainPanel.setLayout(this);
  158.  
  159.         // Create a Frame and put the main panel in it.
  160.         frame = new JFrame("TableExample2");
  161.         frame.addWindowListener(new WindowAdapter() {
  162.             public void windowClosing(WindowEvent e) {System.exit(0);}});
  163.         frame.setBackground(Color.lightGray);
  164.         frame.getContentPane().add(mainPanel);
  165.         frame.pack();
  166.         frame.setVisible(false);
  167.         frame.setBounds(200, 200, 640, 480);
  168.  
  169.     activateConnectionDialog();
  170.     }
  171.  
  172.     public void connect() {
  173.        dataBase = new JDBCAdapter(
  174.             serverField.getText(),
  175.             driverField.getText(),
  176.             userNameField.getText(),
  177.             passwordField.getText());
  178.        sorter.setModel(dataBase);
  179.    }
  180.  
  181.     public void fetch() {
  182.         dataBase.executeQuery(queryTextArea.getText());
  183.     }
  184.  
  185.     public JScrollPane createTable() {
  186.         sorter = new TableSorter();
  187.  
  188.         //connect();
  189.         //fetch();
  190.  
  191.         // Create the table
  192.         JTable table = new JTable(sorter); 
  193.     // Use a scrollbar, in case there are many columns. 
  194.     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
  195.  
  196.         // Install a mouse listener in the TableHeader as the sorter UI.
  197.         sorter.addMouseListenerToHeaderInTable(table);
  198.  
  199.         JScrollPane scrollpane = new JScrollPane(table);
  200.  
  201.         return scrollpane;
  202.     }
  203.  
  204.     public static void main(String s[]) {
  205.         new TableExample2();
  206.     }
  207.  
  208.     public Dimension preferredLayoutSize(Container c){return origin;}
  209.     public Dimension minimumLayoutSize(Container c){return origin;}
  210.     public void addLayoutComponent(String s, Component c) {}
  211.     public void removeLayoutComponent(Component c) {}
  212.     public void layoutContainer(Container c) {
  213.         Rectangle b = c.getBounds();
  214.         int topHeight = 90;
  215.         int inset = 4;
  216.         showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25);
  217.         fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25);
  218.         // queryLabel.setBounds(10, 10, 100, 25);
  219.         queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80);
  220.         tableAggregate.setBounds(new Rectangle(inset,
  221.                                                inset + topHeight,
  222.                                                b.width-2*inset,
  223.                                                b.height-2*inset - topHeight));
  224.     }
  225.  
  226. }
  227.