This is my renderer
class tblCalendarRenderer extends JTextArea implements TableCellRenderer {
JTextArea textField;
public tblCalendarRenderer() {
textField = new JTextArea();
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean selected, boolean focused, int row,
int column) {
textField.setText(value == null ? "" : value.toString());
textField.setLineWrap(true);
textField.setWrapStyleWord(true);
if (column == 0 || column == 6) { // Week-end
textField.setBackground(new Color(255, 220, 220));
} else { // Week
textField.setBackground(new Color(255, 255, 255));
}
if (row % 2 == 0) {
if (value != null) {
if (Integer.parseInt(value.toString()) == realDay
&& currentMonth == realMonth
&& currentYear == realYear) { // Today
textField.setBackground(new Color(220, 220, 255));
}
}
textField.setFont(new java.awt.Font("Dialog",
java.awt.Font.BOLD, 11));
} else {
textField.setFont(new java.awt.Font("Dialog",
java.awt.Font.BOLD, 12));
}
if (selected && row % 2 != 0) {
textField.setBackground(Color.LIGHT_GRAY);
textField.setForeground(Color.black);
}
textField.setBorder(null);
return textField;
}
}
This is the code I tried out to highlight the row in jTextArea. How can i add it into jTable? i tried add textField.addCaretListener(new ExampleCaretListener()); But it will still select whole jTable cell.
class ExampleCaretListener implements CaretListener {
public void caretUpdate(CaretEvent e) {
Color HILIT_COLOR = Color.LIGHT_GRAY;
final Highlighter.HighlightPainter painter;
painter = new DefaultHighlighter.DefaultHighlightPainter(
HILIT_COLOR);
JTextArea textField = (JTextArea) e.getSource();
String lineText = "";
try {
int dot = e.getDot();
int rowStart = Utilities.getRowStart(textField, dot);
int rowEnd = Utilities.getRowEnd(textField, dot);
System.out.println(dot + " " + rowStart + " " + rowEnd);
lineText = textField.getText(rowStart, (rowEnd - rowStart));
textField.getHighlighter().removeAllHighlights();
textField.getHighlighter().addHighlight(rowStart, rowEnd,
painter);
} catch (BadLocationException ex) {
System.out.println(ex);
}
}
}