Path: blob/master/src/demo/share/jfc/TableExample/TableExample.java
41149 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041/**42* A a UI around the JDBCAdaptor, allowing database data to be interactively43* fetched, sorted and displayed using Swing.44*45* NOTE: This example uses a modal dialog via the static convenience methods in46* the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater.47*48* @author Philip Milne49*/50import java.awt.Color;51import java.awt.Component;52import java.awt.Container;53import java.awt.Dimension;54import java.awt.GridLayout;55import java.awt.LayoutManager;56import java.awt.Rectangle;57import java.awt.event.ActionEvent;58import java.awt.event.ActionListener;59import java.awt.event.WindowAdapter;60import java.awt.event.WindowEvent;61import java.util.logging.Level;62import java.util.logging.Logger;63import javax.swing.BoxLayout;64import javax.swing.JButton;65import javax.swing.JComponent;66import javax.swing.JFrame;67import javax.swing.JLabel;68import javax.swing.JOptionPane;69import javax.swing.JPanel;70import javax.swing.JScrollPane;71import javax.swing.JTable;72import javax.swing.JTextArea;73import javax.swing.JTextField;74import javax.swing.UIManager;75import javax.swing.UIManager.LookAndFeelInfo;76import javax.swing.border.BevelBorder;777879public final class TableExample implements LayoutManager {8081static String[] ConnectOptionNames = { "Connect" };82static String ConnectTitle = "Connection Information";83Dimension origin = new Dimension(0, 0);84JButton fetchButton;85JButton showConnectionInfoButton;86JPanel connectionPanel;87JFrame frame; // The query/results window.88JLabel userNameLabel;89JTextField userNameField;90JLabel passwordLabel;91JTextField passwordField;92// JLabel queryLabel;93JTextArea queryTextArea;94JComponent queryAggregate;95JLabel serverLabel;96JTextField serverField;97JLabel driverLabel;98JTextField driverField;99JPanel mainPanel;100TableSorter sorter;101JDBCAdapter dataBase;102JScrollPane tableAggregate;103104/**105* Brigs up a JDialog using JOptionPane containing the connectionPanel.106* If the user clicks on the 'Connect' button the connection is reset.107*/108void activateConnectionDialog() {109if (JOptionPane.showOptionDialog(tableAggregate, connectionPanel,110ConnectTitle,111JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,112null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {113connect();114frame.setVisible(true);115} else if (!frame.isVisible()) {116System.exit(0);117}118}119120/**121* Creates the connectionPanel, which will contain all the fields for122* the connection information.123*/124public void createConnectionDialog() {125// Create the labels and text fields.126userNameLabel = new JLabel("User name: ", JLabel.RIGHT);127userNameField = new JTextField("app");128129passwordLabel = new JLabel("Password: ", JLabel.RIGHT);130passwordField = new JTextField("app");131132serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);133serverField = new JTextField("jdbc:derby://localhost:1527/sample");134135driverLabel = new JLabel("Driver: ", JLabel.RIGHT);136driverField = new JTextField("org.apache.derby.jdbc.ClientDriver");137138139connectionPanel = new JPanel(false);140connectionPanel.setLayout(new BoxLayout(connectionPanel,141BoxLayout.X_AXIS));142143JPanel namePanel = new JPanel(false);144namePanel.setLayout(new GridLayout(0, 1));145namePanel.add(userNameLabel);146namePanel.add(passwordLabel);147namePanel.add(serverLabel);148namePanel.add(driverLabel);149150JPanel fieldPanel = new JPanel(false);151fieldPanel.setLayout(new GridLayout(0, 1));152fieldPanel.add(userNameField);153fieldPanel.add(passwordField);154fieldPanel.add(serverField);155fieldPanel.add(driverField);156157connectionPanel.add(namePanel);158connectionPanel.add(fieldPanel);159}160161public TableExample() {162mainPanel = new JPanel();163164// Create the panel for the connection information165createConnectionDialog();166167// Create the buttons.168showConnectionInfoButton = new JButton("Configuration");169showConnectionInfoButton.addActionListener(new ActionListener() {170171public void actionPerformed(ActionEvent e) {172activateConnectionDialog();173}174});175176fetchButton = new JButton("Fetch");177fetchButton.addActionListener(new ActionListener() {178179public void actionPerformed(ActionEvent e) {180fetch();181}182});183184// Create the query text area and label.185queryTextArea = new JTextArea("SELECT * FROM APP.CUSTOMER", 25, 25);186queryAggregate = new JScrollPane(queryTextArea);187queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));188189// Create the table.190tableAggregate = createTable();191tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));192193// Add all the components to the main panel.194mainPanel.add(fetchButton);195mainPanel.add(showConnectionInfoButton);196mainPanel.add(queryAggregate);197mainPanel.add(tableAggregate);198mainPanel.setLayout(this);199200// Create a Frame and put the main panel in it.201frame = new JFrame("TableExample");202frame.addWindowListener(new WindowAdapter() {203204@Override205public void windowClosing(WindowEvent e) {206System.exit(0);207}208});209frame.setBackground(Color.lightGray);210frame.getContentPane().add(mainPanel);211frame.pack();212frame.setVisible(false);213frame.setBounds(200, 200, 640, 480);214215activateConnectionDialog();216}217218public void connect() {219dataBase = new JDBCAdapter(220serverField.getText(),221driverField.getText(),222userNameField.getText(),223passwordField.getText());224sorter.setModel(dataBase);225}226227public void fetch() {228dataBase.executeQuery(queryTextArea.getText());229}230231public JScrollPane createTable() {232sorter = new TableSorter();233234//connect();235//fetch();236237// Create the table238JTable table = new JTable(sorter);239// Use a scrollbar, in case there are many columns.240table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);241242// Install a mouse listener in the TableHeader as the sorter UI.243sorter.addMouseListenerToHeaderInTable(table);244245JScrollPane scrollpane = new JScrollPane(table);246247return scrollpane;248}249250public static void main(String[] s) {251// Trying to set Nimbus look and feel252try {253for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {254if ("Nimbus".equals(info.getName())) {255UIManager.setLookAndFeel(info.getClassName());256break;257}258}259} catch (Exception ex) {260Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE,261"Failed to apply Nimbus look and feel", ex);262}263264new TableExample();265}266267public Dimension preferredLayoutSize(Container c) {268return origin;269}270271public Dimension minimumLayoutSize(Container c) {272return origin;273}274275public void addLayoutComponent(String s, Component c) {276}277278public void removeLayoutComponent(Component c) {279}280281public void layoutContainer(Container c) {282Rectangle b = c.getBounds();283int topHeight = 90;284int inset = 4;285showConnectionInfoButton.setBounds(b.width - 2 * inset - 120, inset, 120,28625);287fetchButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25);288// queryLabel.setBounds(10, 10, 100, 25);289queryAggregate.setBounds(inset, inset, b.width - 2 * inset - 150, 80);290tableAggregate.setBounds(new Rectangle(inset,291inset + topHeight,292b.width - 2 * inset,293b.height - 2 * inset - topHeight));294}295}296297298