private boolean flag = false; public BlinkCellTable(int numRows, int numColumns) { super(numRows, numColumns);
setDefaultRenderer(Object.class, new BlinkRenderer()); this.refreshThread.start(); } public BlinkCellTable(TableModel dm) { super(dm); setDefaultRenderer(Object.class, new BlinkRenderer()); this.refreshThread.start(); } public static class BlinkRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setForeground(null); setBackground(null); return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } private static class BlinkCell { Color foreground, background; long blinkCount;
public BlinkCell(int blinkCount, Color foreground, Color background) { this.blinkCount = (long)blinkCount*2; this.foreground = foreground; this.background = background; } }
public void blink(int row, int column, int blinkCount, Color foreground, Color background) { synchronized (blinkCellMap) { blinkCellMap.put(new Point(column, row), new BlinkCell(blinkCount, foreground, background)); blinkCellMap.notifyAll(); } }
private class BlinkThread extends Thread { public void run() { while (true) { synchronized (blinkCellMap) { while (blinkCellMap.isEmpty()) { try { blinkCellMap.wait(); } catch (InterruptedException e) {} } flag = ! flag; for (Iterator iter = blinkCellMap.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry)iter.next(); Point cellPos = (Point)entry.getKey(); BlinkCell bc = (BlinkCell) entry.getValue();
bc.blinkCount--; if (bc.blinkCount <= 0) { iter.remove(); }
int column = convertColumnIndexToView(cellPos.x); Rectangle cellRect = getCellRect(cellPos.y, column, false); if (cellRect != null) { repaint(cellRect); } } } try { sleep(500); } catch (InterruptedException e) { } } } } public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component rendererComp = super.prepareRenderer(renderer, row, column);
column = convertColumnIndexToModel(column); Point cellPos = new Point(column, row); BlinkCell bc = (BlinkCell) blinkCellMap.get(cellPos); if (bc != null) { if (flag && bc.foreground != null) { rendererComp.setForeground(bc.foreground); } if (flag && bc.background != null) { rendererComp.setBackground(bc.background); } } return rendererComp; }
public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } BlinkCellTable table = new BlinkCellTable(5, 5); for (int i = 0; i < table.getRowCount(); i++) { for (int k = 0; k < table.getColumnCount(); k++) { table.setValueAt("Hello", i, k); } } table.blink(1, 1, Integer.MAX_VALUE, Color.WHITE, Color.BLACK); table.blink(2, 3, Integer.MAX_VALUE, Color.WHITE, Color.RED); JScrollPane sp = new JScrollPane(table); JFrame f = new JFrame("BlinkTableTest"); f.getContentPane().add(sp, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;public class BlinkCellTable extends JTable {
private Map blinkCellMap = new HashMap();
private Thread refreshThread = new BlinkThread();
private boolean flag = false; public BlinkCellTable(int numRows, int numColumns) {
super(numRows, numColumns);
setDefaultRenderer(Object.class, new BlinkRenderer());
this.refreshThread.start();
} public BlinkCellTable(TableModel dm) {
super(dm);
setDefaultRenderer(Object.class, new BlinkRenderer());
this.refreshThread.start();
} public static class BlinkRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
setForeground(null);
setBackground(null);
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
} private static class BlinkCell {
Color foreground, background;
long blinkCount;
public BlinkCell(int blinkCount, Color foreground, Color background) {
this.blinkCount = (long)blinkCount*2;
this.foreground = foreground;
this.background = background;
}
}
public void blink(int row, int column, int blinkCount, Color foreground, Color background) {
synchronized (blinkCellMap) {
blinkCellMap.put(new Point(column, row), new BlinkCell(blinkCount, foreground, background));
blinkCellMap.notifyAll();
}
}
private class BlinkThread extends Thread {
public void run() {
while (true) {
synchronized (blinkCellMap) {
while (blinkCellMap.isEmpty()) {
try {
blinkCellMap.wait();
} catch (InterruptedException e) {}
}
flag = ! flag;
for (Iterator iter = blinkCellMap.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry)iter.next();
Point cellPos = (Point)entry.getKey();
BlinkCell bc = (BlinkCell) entry.getValue();
bc.blinkCount--; if (bc.blinkCount <= 0) {
iter.remove();
}
int column = convertColumnIndexToView(cellPos.x);
Rectangle cellRect = getCellRect(cellPos.y, column, false);
if (cellRect != null) {
repaint(cellRect);
}
}
}
try {
sleep(500);
} catch (InterruptedException e) {
}
}
}
} public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component rendererComp = super.prepareRenderer(renderer, row, column);
column = convertColumnIndexToModel(column);
Point cellPos = new Point(column, row); BlinkCell bc = (BlinkCell) blinkCellMap.get(cellPos);
if (bc != null) {
if (flag && bc.foreground != null) {
rendererComp.setForeground(bc.foreground);
} if (flag && bc.background != null) {
rendererComp.setBackground(bc.background);
}
}
return rendererComp;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} BlinkCellTable table = new BlinkCellTable(5, 5);
for (int i = 0; i < table.getRowCount(); i++) {
for (int k = 0; k < table.getColumnCount(); k++) {
table.setValueAt("Hello", i, k);
}
}
table.blink(1, 1, Integer.MAX_VALUE, Color.WHITE, Color.BLACK);
table.blink(2, 3, Integer.MAX_VALUE, Color.WHITE, Color.RED); JScrollPane sp = new JScrollPane(table); JFrame f = new JFrame("BlinkTableTest");
f.getContentPane().add(sp, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}