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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * SimpleJXListDemo.java is a 1.4 application that requires no other files. It is derived from
  3.  * SimpleTableDemo in the Swing tutorial.
  4.  */
  5. package org.jdesktop.demo.sample;
  6. import java.awt.BorderLayout;
  7. import java.awt.Color;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.JScrollPane;
  11. import java.awt.FlowLayout;
  12. import java.awt.event.ActionEvent;
  13. import java.io.InputStreamReader;
  14. import java.io.LineNumberReader;
  15. import java.net.URL;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import javax.swing.AbstractAction;
  20. import javax.swing.DefaultListModel;
  21. import javax.swing.JCheckBox;
  22. import javax.swing.ListSelectionModel;
  23. import org.jdesktop.swingx.JXList;
  24. import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
  25. import org.jdesktop.swingx.decorator.Highlighter;
  26. import org.jdesktop.swingx.decorator.HighlighterPipeline;
  27. import org.jdesktop.swingx.decorator.RolloverHighlighter;
  28. /**
  29.  * This SimpleJXListDemo is a very simple example of how to use the extended features of the
  30.  * JXList in the SwingX project. The major features are covered, step-by-step. You can run
  31.  * this demo from the command-line without arguments
  32.  * java org.jdesktop.demo.sample.SimpleJXListDemo
  33.  *
  34.  * If looking at the source, the interesting code is in configureJXList().
  35.  *
  36.  * This is derived from the SimpleTableDemo in the Swing tutorial.
  37.  *
  38.  * @author Patrick Wright (with help from the Swing tutorial :))
  39.  */
  40. public class SimpleJXListDemo extends JPanel {
  41.     public SimpleJXListDemo() {
  42.         super(new BorderLayout());
  43.         initUI();
  44.     }
  45.     
  46.     private void initUI() {
  47.         JXList JXList = initList();
  48.         configureJXList(JXList);
  49.         
  50.         //Create the scroll pane and add the table to it.
  51.         JScrollPane scrollPane = new JScrollPane(JXList);
  52.         
  53.         //Add the scroll pane to this panel.
  54.         add(scrollPane, BorderLayout.CENTER);
  55.         
  56.         // add our search panel
  57.         // TODO: not ready yet
  58.         // add(initSearchPanel(JXList), BorderLayout.NORTH);
  59.         add(initConfigPanel(JXList), BorderLayout.NORTH);
  60.     }
  61.     
  62.     /** Initialize our JXList; this is standard stuff, just as with JTable */
  63.     private JXList initList() {
  64.         // boilerplate table-setup; this would be the same for a JTable
  65.         SampleListModel model = new SampleListModel();
  66.         JXList list = new JXList(model);
  67.         model.loadData();
  68.         
  69.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  70.         return list;
  71.     }
  72.     
  73.     /**
  74.      * For demo purposes, the special features of the JXList are configured here. There is
  75.      * otherwise no reason not to do this in initList().
  76.      */
  77.     private void configureJXList(JXList JXList) {
  78.         // We'll add a highlighter to offset different row numbers
  79.         // Note the setHighlighters() takes an array parameter; you can chain these together.
  80.         JXList.setHighlighters(new HighlighterPipeline(new Highlighter[]{ AlternateRowHighlighter.classicLinePrinter }));
  81.         
  82.         // ...oops! we forgot one
  83.         JXList.getHighlighters().addHighlighter(new RolloverHighlighter(Color.CYAN, Color.WHITE ));
  84.         JXList.setRolloverEnabled(true);
  85.         
  86.         // add a filter--filter on name starting with A, and add a shuttle sort 
  87.         // TODO: not implemented in JXList yet
  88.         // JXList.setFilters(new FilterPipeline(new Filter[] { new PatternFilter("A.*", 0, 0), new ShuttleSorter() }));
  89.     }
  90.     
  91.     /** This shows off some additional JXList configuration, controlled by checkboxes in a Panel. */
  92.     private JPanel initConfigPanel(final JXList JXList) {
  93.         JPanel config = new JPanel();
  94.         FlowLayout fll = (FlowLayout)config.getLayout();
  95.         fll.setAlignment(FlowLayout.LEFT);
  96.         fll.setHgap(30);
  97.         
  98.         
  99.         // This shows or hides the column control--note this is possible at runtime
  100.         final JCheckBox rollover = new JCheckBox();
  101.         rollover.setSelected(JXList.isRolloverEnabled());
  102.         rollover.setAction(new AbstractAction("Rollover") {
  103.             public void actionPerformed(ActionEvent e) {
  104.                 JXList.setRolloverEnabled(rollover.isSelected());
  105.             }
  106.         });
  107.         
  108.         config.add(rollover);
  109.         return config;
  110.     }
  111.     
  112.     /**
  113.      * Create the GUI and show it.  For thread safety,
  114.      * this method should be invoked from the
  115.      * event-dispatching thread.
  116.      */
  117.     private static void createAndShowGUI() {
  118.         //Make sure we have nice window decorations.
  119.         JFrame.setDefaultLookAndFeelDecorated(true);
  120.         
  121.         //Create and set up the window.
  122.         JFrame frame = new JFrame("SimpleJXListDemo");
  123.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  124.         
  125.         //Create and set up the content pane.
  126.         SimpleJXListDemo newContentPane = new SimpleJXListDemo();
  127.         newContentPane.setOpaque(true); //content panes must be opaque
  128.         frame.setContentPane(newContentPane);
  129.         
  130.         //Display the window.
  131.         frame.pack();
  132.         frame.setSize(1024, 768);
  133.         frame.setVisible(true);
  134.     }
  135.     
  136.     public static void main(String[] args) {
  137.         //Schedule a job for the event-dispatching thread:
  138.         //creating and showing this application's GUI.
  139.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  140.             public void run() {
  141.                 createAndShowGUI();
  142.             }
  143.         });
  144.     }
  145.     
  146.     class SampleListModel extends DefaultListModel {
  147.         void loadData() {
  148.             try {
  149.                 URL url = SampleListModel.class.getResource("/org/jdesktop/demo/sample/resources/countries.txt");
  150.                 loadData(url);
  151.             } catch ( Exception e ) {
  152.                 e.printStackTrace();
  153.                 loadDefaultData();
  154.             }
  155.         }
  156.         
  157.         private void loadData(URL url) {
  158.             try {
  159.                 List<String> list = new ArrayList<String>();
  160.                 LineNumberReader lnr = new LineNumberReader(new InputStreamReader(url.openStream()));
  161.                 String line = null;
  162.                 while (( line = lnr.readLine()) != null ) {
  163.                     if ( line.trim().length() > 0 )
  164.                         list.add(line);
  165.                 }
  166.                 Collections.sort(list);
  167.                 for ( String e : list ) {
  168.                     addElement(e);
  169.                 }
  170.             } catch ( Exception e ) {
  171.                 e.printStackTrace();
  172.                 loadDefaultData();
  173.             }
  174.         }
  175.         
  176.         private void loadDefaultData() {
  177.             int colCnt = 6;
  178.             int rowCnt = 10;
  179.             for ( int i=0; i <= rowCnt; i++ ) {
  180.                 addElement( "Row-" + i );
  181.             }
  182.         }
  183.     }
  184. }