jdalbey

Word Renderer Demo

Feb 22nd, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.table.AbstractTableModel;
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5. import java.io.*;
  6. import javax.imageio.ImageIO;
  7. import java.net.*;
  8. import javax.swing.table.DefaultTableCellRenderer;
  9.  
  10. /**
  11.  * This TableDemo is a modified version of the Sun example.
  12.  * The model contains an array of Word (a custom class) instead of Objects.
  13.  * This version contains a TableRenderer class.
  14.  * Sample screenshot: http://i.imgur.com/YXUkljG.png
  15.  */
  16. public class TableRendererDemo extends JPanel
  17. {
  18.     /*
  19.      * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  20.      *
  21.      * Redistribution and use in source and binary forms, with or without
  22.      * modification, are permitted provided that the following conditions
  23.      * are met:
  24.      *
  25.      *   - Redistributions of source code must retain the above copyright
  26.      *     notice, this list of conditions and the following disclaimer.
  27.      *
  28.      *   - Redistributions in binary form must reproduce the above copyright
  29.      *     notice, this list of conditions and the following disclaimer in the
  30.      *     documentation and/or other materials provided with the distribution.
  31.      *
  32.      *   - Neither the name of Oracle or the names of its
  33.      *     contributors may be used to endorse or promote products derived
  34.      *     from this software without specific prior written permission.
  35.      *
  36.      * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  37.      * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  38.      * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39.      * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  40.      * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  41.      * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  42.      * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43.      * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  44.      * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  45.      * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  46.      * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.      */
  48.     private boolean DEBUG = false;
  49.     private ImageIcon starIcon;
  50.  
  51.     public TableRendererDemo()
  52.     {
  53.         super(new GridLayout(1, 0));
  54.         // Load icon images
  55.         try
  56.         {
  57.             // For this demo, we get image from internet, but that's too slow for a real application
  58.             starIcon = new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/PrxOJ6l.png")));
  59.         }
  60.         catch (IOException ex)
  61.         {
  62.            ex.printStackTrace();
  63.         }
  64.         // Setup the JTable
  65.         JTable table = new JTable(new MyTableModel());
  66.         table.setPreferredScrollableViewportSize(new Dimension(250, 70));
  67.         table.setFillsViewportHeight(true);
  68.         table.setRowHeight(32);  // height of icon
  69.         //Set up renderer for the cells containing Words.
  70.         table.setDefaultRenderer(Word.class, new AdvancedWordRenderer());
  71.        
  72.         //Create the scroll pane and add the table to it.
  73.         JScrollPane scrollPane = new JScrollPane(table);
  74.  
  75.         //Add the scroll pane to this panel.
  76.         add(scrollPane);
  77.     }
  78.  
  79.     class MyTableModel extends AbstractTableModel
  80.     {
  81.         private String[] columnNames =
  82.         {
  83.             "",
  84.             ""
  85.         };
  86.         private Word[][] data;
  87.  
  88.         public MyTableModel()
  89.         {
  90.             data = new Word[2][2];
  91.             data[0][0] = new Word("BIG");
  92.             data[1][0] = new Word("TALL");
  93.             data[0][1] = new Word("SMALL");
  94.             data[1][1] = new Word("UNUSUALLYLONG");
  95.         }
  96.  
  97.         public int getColumnCount()
  98.         {
  99.             return columnNames.length;
  100.         }
  101.  
  102.         public int getRowCount()
  103.         {
  104.             return data.length;
  105.         }
  106.  
  107.         public String getColumnName(int col)
  108.         {
  109.             return columnNames[col];
  110.         }
  111.  
  112.         public Object getValueAt(int row, int col)
  113.         {
  114.             return data[row][col];
  115.         }
  116.  
  117.         /*
  118.          * JTable uses this method to determine the default renderer/
  119.          * editor for each cell.  If we didn't implement this method,
  120.          * then the last column would contain text ("true"/"false"),
  121.          * rather than a check box.
  122.          */
  123.         @Override
  124.         public Class getColumnClass(int c)
  125.         {
  126.             return getValueAt(0, c).getClass();
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * Create the GUI and show it.  For thread safety,
  132.      * this method should be invoked from the
  133.      * event-dispatching thread.
  134.      */
  135.     private static void createAndShowGUI()
  136.     {
  137.         //Create and set up the window.
  138.         JFrame frame = new JFrame("TableRendererDemo");
  139.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140.  
  141.         //Create and set up the content pane.
  142.         TableRendererDemo newContentPane = new TableRendererDemo();
  143.         newContentPane.setOpaque(true); //content panes must be opaque
  144.         frame.setContentPane(newContentPane);
  145.  
  146.         //Display the window.
  147.         frame.pack();
  148.         frame.setVisible(true);
  149.     }
  150.  
  151.     public static void main(String[] args)
  152.     {
  153.         //Schedule a job for the event-dispatching thread:
  154.         //creating and showing this application's GUI.
  155.         javax.swing.SwingUtilities.invokeLater(new Runnable()
  156.         {
  157.             public void run()
  158.             {
  159.                 createAndShowGUI();
  160.             }
  161.         });
  162.     }
  163.  
  164.     /** This is our new custom class */
  165.     class Word
  166.     {
  167.         // Note these fields are package-private
  168.         String name;
  169.         int rank;
  170.  
  171.         /** Construct a Word from a given name */
  172.         public Word(String name)
  173.         {
  174.             this.name = name;
  175.             this.rank = name.length();
  176.         }
  177.  
  178.         public String toString()
  179.         {
  180.             return name;
  181.         }
  182.     }
  183.  
  184.  
  185.     /** This Renderer can display EITHER an icon OR text */
  186.     class AdvancedWordRenderer extends DefaultTableCellRenderer
  187.     {
  188.         public void setValue(Object value)
  189.         {
  190.             Word word = (Word) value;
  191.             String name = word.toString();
  192.             setIcon(null);
  193.             setText(null);
  194.             // Long words are displayed as an icon
  195.             if (word.rank > 9)
  196.             {
  197.                 setIcon(starIcon);
  198.                 starIcon.setDescription("star");  // optional
  199.             }
  200.             else
  201.             {
  202.                 setText(name);
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment