Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable color rows 1

Status
Not open for further replies.

grindstonegirl

Technical User
Jun 12, 2003
29
0
0
US
Hi
I have a JTable and i want to change the colors of the rows dynamically. The colors need to correspond to the first letter of the string in the cell. When the string changes, the color also needs to change. I looked up in the java sun tutorial, but i am having some trouble figuring how to get it to work when the string is changed. I know that i have to specify renderers, but i am not sure exactly how to this in my case. thanks for any help you can give.
 
The following code comes from "Core Swing Advanced Programming" (Chapter 6)
=============================
Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class StripedTableCellRenderer implements TableCellRenderer {
	public StripedTableCellRenderer(TableCellRenderer targetRenderer,
									Color evenBack, Color evenFore,
									Color oddBack, Color oddFore) {
		this.targetRenderer = targetRenderer;
		this.evenBack = evenBack;
		this.evenFore = evenFore;
		this.oddBack  = oddBack;
		this.oddFore  = oddFore;
	}

	// Implementation of TableCellRenderer interface
	public Component getTableCellRendererComponent(JTable table,
							Object value, boolean isSelected,
							boolean hasFocus, int row, int column) {
		TableCellRenderer renderer = targetRenderer;
		if (renderer == null) {
			// Get default renderer from the table
			renderer = table.getDefaultRenderer(table.getColumnClass(column));
		}

		// Let the real renderer create the component
		Component comp = renderer.getTableCellRendererComponent(table, value,
													isSelected, hasFocus,
													row, column);

		// Now apply the stripe effect
		if (isSelected == false && hasFocus == false) {
			if ((row & 1) == 0) {
				comp.setBackground(evenBack != null ? evenBack :
										table.getBackground());
				comp.setForeground(evenFore != null ? evenFore :
										table.getForeground());				
			} else {
				comp.setBackground(oddBack != null ? oddBack :
										table.getBackground());
				comp.setForeground(oddFore != null ? oddFore :
										table.getForeground());				
			}
		}

		return comp;
	}

	// Convenience method to apply this renderer to single column
	public static void installInColumn(JTable table, int columnIndex,
							Color evenBack, Color evenFore,
							Color oddBack, Color oddFore) {
		TableColumn tc = table.getColumnModel().getColumn(columnIndex);

		// Get the cell renderer for this column, if any
		TableCellRenderer targetRenderer = tc.getCellRenderer();

		// Create a new StripedTableCellRenderer and install it
		tc.setCellRenderer(new StripedTableCellRenderer(targetRenderer,
										evenBack, evenFore,
										oddBack, oddFore));		
	}

	// Convenience method to apply this renderer to an entire table
	public static void installInTable(JTable table, 
							Color evenBack, Color evenFore,
							Color oddBack, Color oddFore) {
		StripedTableCellRenderer sharedInstance = null;
		int columns = table.getColumnCount();
		for (int i = 0 ; i < columns; i++) {
			TableColumn tc = table.getColumnModel().getColumn(i);
			TableCellRenderer targetRenderer = tc.getCellRenderer();
			if (targetRenderer != null) {
				// This column has a specific renderer
				tc.setCellRenderer(new StripedTableCellRenderer(targetRenderer,
											evenBack, evenFore,
											oddBack, oddFore));
			} else {
				// This column uses a class renderer - use a shared renderer
				if (sharedInstance == null) {
					sharedInstance = new StripedTableCellRenderer(null,
												evenBack, evenFore,
												oddBack, oddFore);
				}
				tc.setCellRenderer(sharedInstance);
			}
		}
	}

	protected TableCellRenderer targetRenderer;
	protected Color evenBack;
	protected Color evenFore;
	protected Color oddBack;
	protected Color oddFore;
}
===========================================
You can add this renderer to your table by the following code :
Code:
// Add the stripe renderer.
StripedTableCellRenderer.installInTable(tbl, Color.lightGray, Color.white, null, null);
============================================
I haven't tested the code. I think you will be able to adapt it to your needs.
Post the result or any problem you have with it.
 
Hey
the code worked great i could easily adapt it to what i wanted. thanks so much!
~grindstone~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top