The Source for Java Technology Collaboration


-- Main.aberrant - 08 May 2008

First draft code sample for coloring an entire row in a JTable.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * RowHighlightDemo:
 * 
 * Demonstrates the use of a TableCellRenderer to color an entire row of a table
 * based on some property of a single column.
 * 
 * @author Collin Fagan
 */
public class RowHighlightDemo extends JPanel {

   private static final long serialVersionUID = 1L;

   private JTable table = new JTable();

   /**
    * sample data column names
    */

   private static final String sportColumn = "Sport";
   private String[] columnNames = { "First Name", "Last Name", sportColumn,
         "# of Years", "Vegetarian" };

   /**
    * sample table data
    */
   private Object[][] data = {
         { "Mary", "Campione", "Snowboarding", 5, Boolean.FALSE },
         { "Alison", "Huml", "Rowing", 3, Boolean.FALSE },
         { "Kathy", "Walrath", "Knitting", 2, Boolean.FALSE },
         { "Sharon", "Zakhour", "Speed reading", 20, Boolean.FALSE },
         { "Philip", "Milne", "Pool", 10, Boolean.FALSE } };

   /**
    * Constructor builds the demo creating the table and associated renderer.
    */
   public RowHighlightDemo() {
      DefaultTableModel model = new DefaultTableModel(data, columnNames);
      table.setModel(model);

      setLayout(new BorderLayout());
      JScrollPane scrollPane = new JScrollPane(table);
      table.setFillsViewportHeight(true);

      RowColorRenderer rowRenderer = new RowColorRenderer();
      for (int i = 0; i < table.getColumnCount(); i++) {
         TableColumn column = table.getColumnModel().getColumn(i);
         column.setCellRenderer(rowRenderer);
      }

      add(scrollPane, BorderLayout.CENTER);
   }

   /**
    * Renderer that looks at the content of the "Sports" columns and colors the
    * cell yellow if the sport column begins with a "S". Since it doesn't look
    * at the value it can be used as the renderer for any column. Since it
    * stores no data between uses it can also be used as the renderer for
    * multiple columns simultaneously.
    */
   private class RowColorRenderer extends DefaultTableCellRenderer {
      @Override
      public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {

         // call super.getTableCellRendererComponent to set up all the
         // default values.
         super.getTableCellRendererComponent(table, value, isSelected,
               hasFocus, row, column);

         int colIndex = table.getColumnModel().getColumnIndex(sportColumn);

         // only color the row if it is unselected
         if (!isSelected) {
            if (table.getValueAt(row, colIndex).toString().startsWith("S")) {
               setBackground(Color.YELLOW);
            } else {
               setBackground(table.getBackground());
            }
         }

         return this;
      }
   }

   /**
    * Start the demo on the Event Dispatch Thread.
    * 
    * @param args
    *            the command line arguments
    */
   public static void main(String[] args) {

      // turn bold fonts off in metal
      UIManager.put("swing.boldMetal", Boolean.FALSE);

      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame demoFrame = new JFrame();
            demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            demoFrame.setContentPane(new RowHighlightDemo());
            demoFrame.pack();
            demoFrame.setVisible(true);
         }
      });
   }
}

Topic JTableColorEntireRowDemo . { Edit | Ref-By | Printable | Diffs r5 < r4 < r3 < r2 < r1 | More }
 XML java.net RSS

  

Revision r5 - 2008-05-18 - 13:27:56 - aberrant
Parents: WebHome > Javatutorials