Creating a Class-Based Custom Cell Renderer in a JTable Component : Table Renderer Editor « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Table Renderer EditorScreenshots 
Creating a Class-Based Custom Cell Renderer in a JTable Component


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Main {
  public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
      public Class getColumnClass(int mColIndex) {
        int rowIndex = 0;
        Object o = getValueAt(rowIndex, mColIndex);
        if (o == null) {
          return Object.class;
        else {
          return o.getClass();
        }
      }
    };
    JTable table = new JTable(model);
    model.addColumn("Col1"new Object[] { Color.red });
    model.addRow(new Object[] { Color.green });
    model.addRow(new Object[] { Color.blue });

    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    
    JFrame f = new JFrame();
    f.setSize(300,300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
  }
}

class ColorTableCellRenderer extends JLabel implements TableCellRenderer {
  Color curColor;
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int rowIndex, int vColIndex) {
    if (curColor instanceof Color) {
      curColor = (Colorvalue;
    else {
      curColor = table.getBackground();
    }
    return this;
  }

  public void paint(Graphics g) {
    g.setColor(curColor);
    g.fillRect(00, getWidth() 1, getHeight() 1);
  }
}

 
Related examples in the same category
1. Shading Rows and Columns in a JTable Component
2. Shades every other column yellow
3. Using Built-In Cell Renderers and Editors in a JTable Component
4. Get Default cell Renderer
5. Get Default Cell Editor
6. Creating a Custom Cell Renderer in a JTable Component
7. A slider renderer for volume values in a tableA slider renderer for volume values in a table
8. Table Cell Editor: ComboBoxCellEditor
9. Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
10. Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
11. StockTable 3: CellRendererStockTable 3: CellRenderer
12. Colored Table CellRendererColored Table CellRenderer
13. Color Table CellRendererColor Table CellRenderer
14. Change Table cell background with column renderer Change Table cell background with column renderer
15. Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
16. A table that allows the user to pick a color from a pulldown listA table that allows the user to pick a color from a pulldown list
17. MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
18. Table Editor: Editable Color ColumnTable Editor: Editable Color Column
19. Tab in and out JComboBox in JTable
20. Creating a Custom Table Cell Editor in a JTable Component
21. Preventing Invalid Values in a Cell in a JTable Component
22. Setting the Activation Click Count for a Table Cell Editor in a JTable Component
23. Using a JComboBox in a Cell in a JTable Component
24. Using a List JSpinner as a Cell Editor in a JTable Component
25. Rendering an image in a JTable column
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.