编程语言

img petehero

实战jTable表格颜色隔行显示

发表于2004/10/12 11:07:00  1348人阅读

分类: Java



使用static方法,放在常用工具类myUtil中,
只需在表格数据生成后加入myUtil.makeFace(jTable1);即可:


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));  //设置偶数行底色
                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();
        }

}


//...省略jTable数据显示代码...
//表格数据生成后加入:

myUtil.makeFace(jTable1);




http://bjgx.nease.net

阅读全文
0 0

相关文章推荐

img
取 消
img