Use cells to draw two java tables

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.