JTable 單獨設置某一行顏色

public static void makeFace(JTable table) {
        try {
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {

                public Component getTableCellRendererComponent(JTable table,
                        Object value, boolean isSelected, boolean hasFocus,
                        int row, int column) {
                    if (row % 2 == 0) {
                        setBackground(Color.white); //設置奇數行底色
                    } else if (row % 2 == 1) {
                        setBackground(new Color(206, 231, 255)); //設置偶數行底色
                    }
                    if (Double.parseDouble(table.getValueAt(row, 11).toString()) > 0) {
                        setBackground(Color.red);
                    }
                    //如果需要設置某一個Cell顏色,需要加上column過濾條件即可
                    return super.getTableCellRendererComponent(table, value,
                            isSelected, hasFocus, row, column);
                }
            };
            for (int i = 0; i < table.getColumnCount(); i++) {
                table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章