Background color rendering of CheckBox cells in JTable

Source: Internet
Author: User

Java has been around for many years, but it is still a little daunting when it comes to JTABLE operations. This project encountered background color rendering for the CheckBox cell, which really gave me a hard time. Here are the solutions to share with the students who need them. [Java] for (int I = 0; I <table. getColumnCount (); I ++) {// The warning flag is displayed in the column 12th. Use the CheckBox control to display if (I = 11) {table. getColumn (table. getModel (). getColumnName (I )). setCellRenderer (new TableCellRenderer () {private JCheckBox checkBox = new JCheckBox (); @ Override public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {if (value! = Null) {checkBox. setSelected (Boolean. valueOf (value. toString (); checkBox. setHorizontalAlignment (JCheckBox. CENTER); if (isSelected) {// the checkBox color is selected by default. setBackground (new Color (10, 36,106);} else {String warn_Flag = (String) table. getModel (). getValueAt (row, 13); if ("1 ". equals (warn_Flag) {checkBox. setBackground (Color. RED);} else {checkBox. setBackground (Color. white) ;}}return checkBox ;}}) ;}else {table. getColumn (table. getModel (). getColumnName (I )). setCellRenderer (new DefaultTableCellRenderer () {@ Override public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {String warn_Flag = (String) table. getModel (). getValueAt (row, 13); if ("1 ". equals (warn_Flag) {setBackground (Color. RED);} else {setBackground (Color. white);} return super. getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column) ;}); <span style = "white-space: pre" ></span> }}

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.