[ Back | Previous | Next ]

How to user TableCellRenderer?

Package:
javax.swing.table.*
Product:
Swing
Release:
1.0.3
Related Links:
AbstractTableModel
JTable
TableCellRenderer
TableColumn
Comment:
To apply a custom CellRenderer use

>>>>
	setFixedTable(new JTable(getFixedModel()) {
		public void valueChanged(ListSelectionEvent e) {
			super.valueChanged(e);
			checkSelection(true);
		}
	});
	setTable(new JTable(getTableModel()) {
		public void valueChanged(ListSelectionEvent e) {
			super.valueChanged(e);
			checkSelection(false);
		}
	});
	//
	getTable().setDefaultRenderer(Object.class, new TextFormatCellRenderer() );
>>>>>

The code for a CellRenderer could be….
>>>>>>
package bmc88.swing;

import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.table.*;
import java.awt.*;
import java.util.*;
import java.text.*;
/**
 * This type was created in VisualAge.
 */
public class TextFormatCellRenderer extends JLabel implements TableCellRenderer {
	private String fieldFormat = new String();
	private CellFormatAttribute cellFormats = new CellFormatAttribute();
/**
 * Constructor
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
public TextFormatCellRenderer() {
	super();
	initialize();
}
/**
 * TextFormatCellRenderer constructor comment.
 * @param image com.sun.java.swing.Icon
 */
public TextFormatCellRenderer(Icon image) {
	super(image);
}
/**
 * TextFormatCellRenderer constructor comment.
 * @param image com.sun.java.swing.Icon
 * @param horizontalAlignment int
 */
public TextFormatCellRenderer(Icon image, int horizontalAlignment) {
	super(image, horizontalAlignment);
}
/**
 * TextFormatCellRenderer constructor comment.
 * @param text java.lang.String
 */
public TextFormatCellRenderer(String text) {
	super(text);
}
/**
 * TextFormatCellRenderer constructor comment.
 * @param text java.lang.String
 * @param horizontalAlignment int
 */
public TextFormatCellRenderer(String text, int horizontalAlignment) {
	super(text, horizontalAlignment);
}
/**
 * TextFormatCellRenderer constructor comment.
 * @param text java.lang.String
 * @param icon com.sun.java.swing.Icon
 * @param horizontalAlignment int
 */
public TextFormatCellRenderer(String text, Icon icon, int horizontalAlignment) {
	super(text, icon, horizontalAlignment);
}
/**
 * This method was created in VisualAge.
 * @return bmc88.swing.CellFormatAttribute
 */
public CellFormatAttribute getCellFormats() {
	return cellFormats;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
	Color foreground = null;
	Color background = null;
	Font font = null;
	TableModel model = table.getModel();
	if (isSelected) {
		setForeground((foreground != null) ? foreground : table.getSelectionForeground());
		setBackground(table.getSelectionBackground());
	} else {
		setForeground((foreground != null) ? foreground : table.getForeground());
		setBackground((background != null) ? background : table.getBackground());
	}
	setFont((font != null) ? font : table.getFont());
	setValue(value);
	
	return this;
}
/**
 * Called whenever the part throws an exception.
 * @param exception java.lang.Throwable
 */
private void handleException(Throwable exception) {

	/* Uncomment the following lines to print uncaught exceptions to stdout */
	// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
	// exception.printStackTrace(System.out);
}
/**
 * Initialize the class.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
	// user code begin {1}
	// user code end
	setName("TextFormatCellRenderer");
	setOpaque(true);
	setText("TextFormatCellRenderer");
	setSize(125, 30);
	// user code begin {2}
	// user code end
}
/**
 * main entrypoint - starts the part when it is run as an application
 * @param args java.lang.String[]
 */
public static void main(java.lang.String[] args) {
	try {
		Frame frame;
		try {
			Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame");
			frame = (Frame)aFrameClass.newInstance();
		} catch (java.lang.Throwable ivjExc) {
			frame = new Frame();
		}
		TextFormatCellRenderer aTextFormatCellRenderer;
		aTextFormatCellRenderer = new TextFormatCellRenderer();
		frame.add("Center", aTextFormatCellRenderer);
		frame.setSize(aTextFormatCellRenderer.getSize());
		frame.setVisible(true);
	} catch (Throwable exception) {
		System.err.println("Exception occurred in main() of bmc88.swing.TextFormatCellRenderer");
		exception.printStackTrace(System.out);
	}
}
/**
 * This method was created in VisualAge.
 * @param newValue bmc88.swing.CellFormatAttribute
 */
public void setCellFormats(CellFormatAttribute newValue) {
	this.cellFormats = newValue;
}
protected void setValue(Object value) {
	Class vClass = value.getClass();
	String format = (String) getCellFormats().getAttributeTable().get(vClass);
	if (format == null) {
		setText((value == null) ? "" : value.toString());
	} else {
		if (vClass == Integer.class || vClass == Double.class || vClass == Long.class) {
			DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
			df.applyPattern(format);
			setText(df.format(value));
		} else
			if (vClass == Date.class) {
			} else {
			}
	}
}
}>>>>>>
1