package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.*;

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());

        //JTable table = new HighlightableTable(model);
        JTable table = new JTable(new DefaultTableModel(10, 10));
        //table.setAutoCreateRowSorter(true);
        table.setRowSelectionAllowed(false);

        HighlightListener highlighter = new HighlightListener(table);
        table.addMouseListener(highlighter);
        table.addMouseMotionListener(highlighter);

        table.setDefaultRenderer(Object.class, new HighlightRenderer(highlighter));
        table.setDefaultRenderer(Number.class, new HighlightRenderer(highlighter));

        add(new JScrollPane(table));
        setPreferredSize(new Dimension(320, 240));
    }
    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class HighlightListener extends MouseAdapter {
    private static final Color HIGHLIGHT1 = new Color(200, 200, 255);
    private static final Color HIGHLIGHT2 = new Color(240, 240, 255);
    private int row = -1;
    private int col = -1;
    private final JTable table;

    public HighlightListener(JTable table) {
        super();
        this.table = table;
    }
    public Color getHighlightableCellColor(int row, int column) {
        if (this.row == row || this.col == column) {
            if (this.row == row && this.col == column) {
                return HIGHLIGHT1;
            } else {
                return HIGHLIGHT2;
            }
        }
        return null;
    }
    private void setHighlighTableCell(Point pt) {
        row = table.rowAtPoint(pt);
        col = table.columnAtPoint(pt);
        if (row < 0 || col < 0) {
            row = -1;
            col = -1;
        }
        table.repaint();
    }
    @Override public void mouseMoved(MouseEvent e) {
        setHighlighTableCell(e.getPoint());
    }
    @Override public void mouseDragged(MouseEvent e) {
        setHighlighTableCell(e.getPoint());
    }
    @Override public void mouseExited(MouseEvent e) {
        row = -1;
        col = -1;
        table.repaint();
    }
}

class HighlightRenderer extends DefaultTableCellRenderer {
    private final transient HighlightListener highlighter;

    public HighlightRenderer(HighlightListener highlighter) {
        super();
        this.highlighter = highlighter;
    }
    @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setHorizontalAlignment(value instanceof Number ? RIGHT : LEFT);
        Color highlight = highlighter.getHighlightableCellColor(row, column);
        if (Objects.nonNull(highlight)) {
            setBackground(highlight);
        } else {
            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        }
        return this;
    }
}

// class HighlightableTable extends JTable {
//     private final HighlightListener highlighter;
//     public HighlightableTable(TableModel model) {
//         super(model);
//         highlighter = new HighlightListener(this);
//         addMouseListener(highlighter);
//         addMouseMotionListener(highlighter);
//     }
//     @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
//         Component c = super.prepareRenderer(renderer, row, column);
//         if (highlighter.isHighlightableCell(row, column)) {
//             c.setBackground(Color.RED);
//         } else if (isRowSelected(row)) {
//             c.setBackground(getSelectionBackground());
//         } else {
//             c.setBackground(Color.WHITE);
//         }
//         return c;
//     }
// }

@@ -152,22 +152,23 @@
 a.util.O
-bjects
+ptional
 ;%0Aimport
@@ -2041,37 +2041,40 @@
 le table;%0A%0A    p
-ublic
+rotected
  HighlightListen
@@ -2154,21 +2154,41 @@
  public 
+Optional%3C? extends 
 Color
+%3E
  getHigh
@@ -2354,32 +2354,44 @@
          return 
+Optional.of(
 HIGHLIGHT1;%0A    
@@ -2384,16 +2384,17 @@
 GHLIGHT1
+)
 ;%0A      
@@ -2431,16 +2431,28 @@
  return 
+Optional.of(
 HIGHLIGH
@@ -2453,16 +2453,17 @@
 GHLIGHT2
+)
 ;%0A      
@@ -2495,20 +2495,32 @@
  return 
-null
+Optional.empty()
 ;%0A    %7D%0A
@@ -3215,29 +3215,32 @@
 hter;%0A%0A    p
-ublic
+rotected
  HighlightRe
@@ -3670,25 +3670,53 @@
     
-Color highlight =
+setBackground(table.getBackground());%0A       
  hig
@@ -3766,212 +3766,42 @@
 umn)
-;%0A        if (Objects.nonNull(highlight)) %7B%0A            setBackground(highlight);%0A        %7D else %7B%0A            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());%0A        %7D
+.ifPresent(c -%3E setBackground(c));
 %0A   
@@ -3932,21 +3932,24 @@
 //     p
-ublic
+rotected
  Highlig

use Optional instead of null check