Table Editor: Editable Color Column : 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 
Table Editor: Editable Color Column
Table Editor: Editable Color Column
 
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

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

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

public class EditableColorColumn {

  public static void main(String args[]) {

    Color choices[] Color.red, Color.orange, Color.yellow, Color.green,
        Color.blue, Color.magenta };
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox comboBox = new JComboBox(choices);
    comboBox.setRenderer(renderer);
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("Editable Color Table");
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);

    TableColumn column = table.getColumnModel().getColumn(3);
    column.setCellRenderer(renderer);
    column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400150);
    frame.setVisible(true);
  }
}

class ComboTableCellRenderer implements ListCellRenderer, TableCellRenderer {
  DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();

  DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();

  private void configureRenderer(JLabel renderer, Object value) {
    if ((value != null&& (value instanceof Color)) {
      renderer.setIcon(new DiamondIcon((Colorvalue));
      renderer.setText("");
    else {
      renderer.setIcon(null);
      renderer.setText((Stringvalue);
    }
  }

  public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
    listRenderer = (DefaultListCellRendererlistRenderer
        .getListCellRendererComponent(list, value, index, isSelected,
            cellHasFocus);
    configureRenderer(listRenderer, value);
    return listRenderer;
  }

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    tableRenderer = (DefaultTableCellRenderertableRenderer
        .getTableCellRendererComponent(table, value, isSelected,
            hasFocus, row, column);
    configureRenderer(tableRenderer, value);
    return tableRenderer;
  }
}

class ColorTableModel extends AbstractTableModel {

  Object rowData[][] { { "1""ichi - \u4E00", Boolean.TRUE, Color.red },
      "2""ni - \u4E8C", Boolean.TRUE, Color.blue },
      "3""san - \u4E09", Boolean.FALSE, Color.green },
      "4""shi - \u56DB", Boolean.TRUE, Color.magenta },
      "5""go - \u4E94", Boolean.FALSE, Color.pink }};

  String columnNames[] "English""Japanese""Boolean""Color" };

  public int getColumnCount() {
    return columnNames.length;
  }

  public String getColumnName(int column) {
    return columnNames[column];
  }

  public int getRowCount() {
    return rowData.length;
  }

  public Object getValueAt(int row, int column) {
    return rowData[row][column];
  }

  public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());
  }

  public void setValueAt(Object value, int row, int column) {
    rowData[row][column= value;
  }

  public boolean isCellEditable(int row, int column) {
    return (column != 0);
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}

           
         
  
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. Creating a Class-Based Custom Cell Renderer in a JTable Component
8. A slider renderer for volume values in a tableA slider renderer for volume values in a table
9. Table Cell Editor: ComboBoxCellEditor
10. Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
11. Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
12. StockTable 3: CellRendererStockTable 3: CellRenderer
13. Colored Table CellRendererColored Table CellRenderer
14. Color Table CellRendererColor Table CellRenderer
15. Change Table cell background with column renderer Change Table cell background with column renderer
16. Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
17. 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
18. MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
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.