Subscribe:

Main Menu

Senin, April 21, 2008

Buat JTable jadi slang sling warnanya :D

Lihat Program-Program yang lain itu jadi ngiri coz keliatan bagus kalo di tabelnya itu bisa selang-seling warnanya.. jadi kepikiran gimana ya agar tabelku juga bisa kayak gitu :D. setelah otak-atik akhirnya ketemu juga. aku tambahi aja di bagian preparerenderernya di JTable... jadinya kayak gini dech :D

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Object value = null;
ColumnPaintListener columnPaintListener = null;
Column dataSetColumn = null;
Variant variantCopy = null;
if (dbTableModel != null) {
try {
dataSetColumn = dbTableModel.getColumn(convertColumnIndexToModel(column));
dataSet.getDisplayVariant(dataSetColumn.getOrdinal(), row, (Variant) variantValue);

if (renderer instanceof TableFastStringRenderer) {
value = dataSetColumn.format(variantValue);
variantCopy = (Variant) variantValue.clone();
columnPaintListener = dataSetColumn.getColumnPaintListener();
} else if (dataSetColumn.getDataType() == Variant.INPUTSTREAM &&
renderer instanceof TableImageRenderer) {
InputStream inputStream = variantValue.getInputStream();
inputStream.reset();
if (DBUtilities.isBMPFile(inputStream) ||
DBUtilities.isGIForJPGFile(inputStream)) {
if (imageCache == null) {
imageCache = new LRUCache();
}
if ((value = imageCache.get(inputStream)) == null) {
if (DBUtilities.isGIForJPGFile(inputStream)) {
byte [] bytes = DBUtilities.getByteArrayFromStream(inputStream);
if (bytes == null) {
value = new TextIcon(Res._UnsupImg);
} else {
value = new ImageIcon(bytes);
}
} else {
Image bmpImage = DBUtilities.makeBMPImage(inputStream);
if (bmpImage != null) {
value = new ImageIcon(bmpImage);
} else {
value = new TextIcon(Res._UnsupImg);
}
}
if (value != null) {
imageCache.put(inputStream, value);
}
}
} else {
if (variantValue.isNull()) {
value = new TextIcon(" ");
} else {
value = new TextIcon(Res._UnsupImg);
}
}
} else {
value = getValueAt(row, column);
}
}
// catch DataSetException or IOException
catch (Exception e) {
DBExceptionHandler.handleException(dataSet, this, e);
}
} else {
value = getValueAt(row, column);
}
boolean rowIsAnchor = (selectionModel.getAnchorSelectionIndex() == row);
boolean colIsAnchor =
(columnModel.getSelectionModel().getAnchorSelectionIndex() == column);
boolean hasFocus = (rowIsAnchor && colIsAnchor) && hasFocus();
boolean isSelected = hasFocus || isCellSelected(row, column);
if(getColumnClass(column).equals(Boolean.class) && (value instanceof String)){
value=false;
}
Component component = renderer.getTableCellRendererComponent(this, value,
isSelected, hasFocus,
row, column);
if (dbTableModel != null &&
columnPaintListener != null && component instanceof CustomPaintSite) {
Color foreground = component.getForeground();
Color background = component.getBackground();
columnPaintListener.painting(dataSet, dataSetColumn, row, variantValue, (CustomPaintSite) component);
if (variantCopy != null && !variantCopy.equals(variantValue) &&
component instanceof TableFastStringRenderer) {
try {
((TableFastStringRenderer) component).setValue(dataSetColumn.format(variantValue));
} catch (Exception e) {
DBExceptionHandler.handleException(dataSet, this, e);
}
}

if (isSelected) {
component.setForeground(foreground);
component.setBackground(background);
}
}

if(row%2!=0 && !isSelected)
component.setBackground(slangsling);

return component;
}