JTable cells are drawn by the cell painter, which is a class for executing the TableCellRenderer interface. The TableCellRenderer interface defines the unique getTableCellRendererComponent method, returns a reference to Component, and the second Component is used for drawing operations. However, it is easier to define a separate class to execute TableCellRenderer and implement drawing operations. Therefore, TablecellRenderer generally only returns a reference to itself. The following are the parameters passed to the getTableCellRendererComponent () method:
- References to JTable include the cells to be drawn
- Reference to cell values
- Boolean flag, used to indicate whether a cell is selected
- Boolean indicates whether the cell is the input focus.
- Row Index Number of the drawn Cell
- Column Index Number of the cell to be drawn
DefaultTableCellHeaderRenderer headerRender = serialVersionUID = 7012909291394255134l isSelected, hasFocus, row, setBorder )? "" SetBackground (Color (0x2d7e89 setFont (Font ("", Font. PLAIN, 15 tables. getTableHeader (). setBackground (Color (0x2d7e89 DefaultTableCellRenderer cellRender = serialVersionUID =-incluisselected, hasFocus, row, setBackground (Color (0x6EBBCA setBorder (MatteBorder, color (0x1A2730 setBackground (Color (0 xDCEBED setText (value = )? "" SetFont (Font ("", Font. BOLD, 14 table. setDefaultRenderer (Object. table. setShowGrid (table. setShowHorizontalLines (table. setBackground (Color (0 xDCEBED table. setIntercellSpacing (Dimension (0, 0 table. getColumn (colname [colname. length-1]). setMaxWidth (0 table. getColumn (colname [colname. length-1]). setMinWidth (0 table. getColumn (colname [colname. length-1]). setPreferredWidth (0}
Running result
By using the cell painter, you can set the color font of the specified cell or the specified row in the specified column, or even display different colors based on the cell value.
Sample Code:
DefaultTableCellHeaderRenderer headerRender = serialVersionUID = 7012909291394255134l isSelected, hasFocus, row, setBorder )? "" SetBackground (Color (0x2d7e89 setFont (Font ("", Font. PLAIN, 15 tables. getTableHeader (). setBackground (Color (0x2d7e89 DefaultTableCellRenderer cellRender = serialVersionUID =-4177459616324121906l isSelected, hasFocus, row, var = Integer. valueOf (table. getModel (). getValueAt (row, 0 (var> = 5 & var <= 7} setBackground (Color (0 xDCEBED setBorder (MatteBorder (, Color (0x1A2730 setText (value = )? "" SetFont (Font ("", Font. BOLD, 14 table. setDefaultRenderer (Object. table. setShowGrid (table. setShowHorizontalLines (table. setBackground (Color (0 xDCEBED table. setIntercellSpacing (Dimension (0, 0 table. getColumn (colname [colname. length-1]). setMaxWidth (0 table. getColumn (colname [colname. length-1]). setMinWidth (0 table. getColumn (colname [colname. length-1]). setPreferredWidth (0}
Running result