AutoCompleteDemoPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. package org.jdesktop.demo.swingx;
  2. import javax.swing.DefaultComboBoxModel;
  3. import javax.swing.DefaultListModel;
  4. import org.jdesktop.demo.DemoPanel;
  5. import org.jdesktop.swingx.autocomplete.*;
  6. /**
  7.  *
  8.  * @author  Thomas Bierhance
  9.  */
  10. public class AutoCompleteDemoPanel extends DemoPanel {
  11.     
  12.     final static String htmlDescription =
  13.             "<html><b>Automatic Completion Demo</b><br/>" +
  14.             "Demonstrates the use of the &quot;autocomplete&quot; package " +
  15.             "to enable automatic completion for some components: JComboBox " +
  16.             "and JTextField/JList<br/>" +
  17.             "Note that the strict version only allows items from the list " +
  18.             "to be entered while the non-strict version allows any item." +
  19.             "</html>";
  20.     
  21.     String[] items = new String[] {
  22.         "Alexander", "Artus", "Axel", "Bastian", "Benno", "Bj鰎n",
  23.                 "Carl Philipp", "Carsten", "Felix", "Fleur", "Florian", "Frederik",
  24.                 "Jan", "Jonas", "Julian", "Kai", "Karsten", "Markus", "Matthias",
  25.                 "Moritz", "Nico", "Norman", "Plamen", "Sandra", "Stefan", "Thorsten",
  26.                 "Tobias", "Werner"
  27.     };
  28.     
  29.     public AutoCompleteDemoPanel() {
  30.         setName("Automatic Completion Demo");
  31.         initComponents();
  32.         strictComboBox.setModel(new DefaultComboBoxModel(items));
  33.         nonStrictComboBox.setModel(new DefaultComboBoxModel(items));
  34.         
  35.         Configurator.enableAutoCompletion(strictComboBox);
  36.         Configurator.enableAutoCompletion(nonStrictComboBox);
  37.         
  38.         DefaultListModel listModel = new DefaultListModel();
  39.         for (int i=0,n=items.length;i<n;i++) {
  40.             listModel.add(i,items[i]);
  41.         }
  42.         list.setModel(listModel);
  43.         Configurator.enableAutoCompletion(list, textField);
  44.         //textField.setText(nonStrictComboBox.getSelectedItem().toString());
  45.         
  46.     }
  47.     
  48.     public String getHtmlDescription() {
  49.         return htmlDescription;
  50.     }
  51.     
  52.     public String getName() {
  53.         return "Autocomplete";
  54.     }
  55.     
  56.     /** This method is called from within the constructor to
  57.      * initialize the form.
  58.      * WARNING: Do NOT modify this code. The content of this method is
  59.      * always regenerated by the Form Editor.
  60.      */
  61.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  62.     private void initComponents() {
  63.         java.awt.GridBagConstraints gridBagConstraints;
  64.         strictComboBoxLabel = new javax.swing.JLabel();
  65.         strictComboBox = new javax.swing.JComboBox();
  66.         nonStrictComboBoxLabel = new javax.swing.JLabel();
  67.         nonStrictComboBox = new javax.swing.JComboBox();
  68.         separator = new javax.swing.JSeparator();
  69.         textFieldLabel = new javax.swing.JLabel();
  70.         textField = new javax.swing.JTextField();
  71.         listLabel = new javax.swing.JLabel();
  72.         listScrollPane = new javax.swing.JScrollPane();
  73.         list = new javax.swing.JList();
  74.         setLayout(new java.awt.GridBagLayout());
  75.         strictComboBoxLabel.setText("JComboBox (strict)");
  76.         gridBagConstraints = new java.awt.GridBagConstraints();
  77.         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
  78.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  79.         add(strictComboBoxLabel, gridBagConstraints);
  80.         gridBagConstraints = new java.awt.GridBagConstraints();
  81.         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
  82.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  83.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  84.         add(strictComboBox, gridBagConstraints);
  85.         nonStrictComboBoxLabel.setText("JComboBox (non-strict)");
  86.         gridBagConstraints = new java.awt.GridBagConstraints();
  87.         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
  88.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  89.         add(nonStrictComboBoxLabel, gridBagConstraints);
  90.         nonStrictComboBox.setEditable(true);
  91.         gridBagConstraints = new java.awt.GridBagConstraints();
  92.         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
  93.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  94.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  95.         add(nonStrictComboBox, gridBagConstraints);
  96.         gridBagConstraints = new java.awt.GridBagConstraints();
  97.         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
  98.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  99.         gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0);
  100.         add(separator, gridBagConstraints);
  101.         textFieldLabel.setText("JTextField (strict)");
  102.         gridBagConstraints = new java.awt.GridBagConstraints();
  103.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  104.         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
  105.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  106.         add(textFieldLabel, gridBagConstraints);
  107.         gridBagConstraints = new java.awt.GridBagConstraints();
  108.         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
  109.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  110.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  111.         add(textField, gridBagConstraints);
  112.         listLabel.setText("JList");
  113.         gridBagConstraints = new java.awt.GridBagConstraints();
  114.         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
  115.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  116.         add(listLabel, gridBagConstraints);
  117.         listScrollPane.setViewportView(list);
  118.         gridBagConstraints = new java.awt.GridBagConstraints();
  119.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  120.         gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
  121.         add(listScrollPane, gridBagConstraints);
  122.     }
  123.     // </editor-fold>//GEN-END:initComponents
  124.     
  125.     
  126.     // Variables declaration - do not modify//GEN-BEGIN:variables
  127.     private javax.swing.JList list;
  128.     private javax.swing.JLabel listLabel;
  129.     private javax.swing.JScrollPane listScrollPane;
  130.     private javax.swing.JComboBox nonStrictComboBox;
  131.     private javax.swing.JLabel nonStrictComboBoxLabel;
  132.     private javax.swing.JSeparator separator;
  133.     private javax.swing.JComboBox strictComboBox;
  134.     private javax.swing.JLabel strictComboBoxLabel;
  135.     private javax.swing.JTextField textField;
  136.     private javax.swing.JLabel textFieldLabel;
  137.     // End of variables declaration//GEN-END:variables
  138.     
  139. }