参考:
http://www2.opensource.com.cn*********************************************
欢迎提供技术资料(译文,原创)
http://www2.opensource.com.cn/
*********************************************

解决方案 »

  1.   

    to  iceandfire(【OPENSOURCE】) :那个论坛我注册了,可是登入不了啊,还有什么资料啊,其他高手跑哪里去了???
      

  2.   

    用cell renderer
    看我的例子吧。自己再改一下import com.bradea.configeditor.bean.Bean;
    import com.bradea.configeditor.table.model.CommonTableModel;
    import java.awt.Color;
    import java.awt.Component;
    import java.util.List;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableModel;public class CommonCellRenderer extends JLabel implements TableCellRenderer {    /**
         * The foreground color of deleted record.
         */
        protected static final Color DELETED_CELL_FOREGROUND = Color.LIGHT_GRAY;    /**
         * The foreground color of modified record.
         */
        protected static final Color MODIFIED_CELL_FOREGROUND = Color.RED;    /**
         * The foreground color when cell has focus.
         */
        protected static final Color FOCUS_CELL_FOREGROUND = UIManager.getColor("Table.focusCellForeground");    /**
         * The background color when cell has focus.
         */
        protected static final Color FOCUS_CELL_BACKGROUND = UIManager.getColor("Table.focusCellBackground");    /**
         * The border when the cell has focus.
         */
        protected static final Border sHasFocusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");    /**
         * The border when the cell has no focus.
         */
        protected static final Border sNoFocusBorder = new EmptyBorder(1, 1, 1, 1);    /**
         * This interface defines the method required by any object that would like
         * to be a renderer for cells in a JTable.
         *
         * @param table The JTable that is asking the renderer to draw.
         * @param value The value of the cell to be rendered.
         * @param isSelected This interface defines the method required by any
         *        object that would like to be a renderer for cells in a JTable.
         * @param hasFocus If true, render cell appropriately. For example, put a
         *        special border on the cell, if the cell can be edited, render in
         *        the color used to indicate editing.
         * @param row The row index of the cell being drawn. When drawing the
         *        header, the value of row is -1.
         * @param column The column index of the cell being drawn.
         */
        public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {
            setOpaque(true);
            setFont(table.getFont());
            setValue(value);        // Set background color and foreground color when the cell is selected.
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                setBackground(table.getSelectionBackground());
            } else {
                TableModel model = table.getModel();
                List dataList = ((CommonTableModel) model).getDataList();
                Bean tempBean = (Bean) dataList.get(row);
                if (tempBean.isModified()) {
                    setForeground(MODIFIED_CELL_FOREGROUND);
                } else if (tempBean.isDeleted()) {
                    setForeground(DELETED_CELL_FOREGROUND);
                } else {
                    setForeground(table.getForeground());
                }
                setBackground(table.getBackground());
            }        // Set background color and foreground color when the cell has focus.
            if (hasFocus) {
                setBorder(sHasFocusBorder);
                if (table.isCellEditable(row, column)) {
                    setForeground(FOCUS_CELL_FOREGROUND);
                    setBackground(FOCUS_CELL_BACKGROUND);
                }
            } else {
                setBorder(sNoFocusBorder);
            }        return this;
        }    /**
         * Set the text for the cell.
         *
         * @param value The value to be set to the cell.
         */
        protected void setValue(Object value) {
            setText(value != null ? value.toString() : "");
        }
    }
      

  3.   

    这个两个class 是什么啊???能否把思路告诉我一下
      

  4.   

    烤!来晚了一步。不过我的更灵活。
    import javax.swing.table.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;/**
     * <p>Title: 表格颜色渲染器</p>
     * <p>Description: 实现表格不同格子显示不同颜色的功能。</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: PCI</p>
     * @author 林楚光
     * @version 1.0.0.6
     */class TableCellColorRenderer
        extends JLabel
        implements TableCellRenderer
    {
      CellColorInterface cellColorInterface;
      /**
       * 构造函数,引入CellColorInterface接口的实现实例。
       * @param cellColorInterface CellColorInterface接口的实现实例
       */
      public TableCellColorRenderer(CellColorInterface cellColorInterface)
      {
        super();
        this.cellColorInterface=cellColorInterface;
        setOpaque(true);
      }  /**每当表格的ui要update的时候,会调用本方法取得格子的渲染器。
       * @param table :目标表格
       * @param color :默认颜色
       * @param isSelected :格子是否被选中
       * @param hasFocus :各自是否正获得焦点
       * @param row :格子所在的行
       * @param column :个子所在的列
       * @return :设置好的渲染器。
       */
      public Component getTableCellRendererComponent(
          JTable table, Object color,
          boolean isSelected, boolean hasFocus,
          int row, int column)
      {
        this.setBackground(cellColorInterface.getColor(row,column,isSelected));
        this.setText(cellColorInterface.getText(row,column,isSelected));
        return this;
      }
    }
    接口文件:
    import java.awt.*;/**
     * <p>Title: 表格颜色接口类</p>
     * <p>Description: 一个接口,实现表格中格子显示不同颜色的功能。</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: PCI</p>
     * @author 林楚光
     * @version 1.0.0.6
     */public interface CellColorInterface
    {
      /**
       * 设置表格中每一格的底色。
       * @param row 格子所在的行
       * @param col 格子所在的列
       * @param isSelected 是否被选中
       * @return 格子希望的颜色
       */
      public Color getColor(int row,int col,boolean isSelected);
      /**
       * 设置表格中每一格的内容。
       * @param row 格子所在的行
       * @param col 格子所在的列
       * @param isSelected 是否被选中
       * @return 格子希望的内容(String型)
       */
      public String getText(int row,int col,boolean isSelected);
    }
      

  5.   

    to xxlcg(☆突突☆) 
    好像有点看懂了,具体怎么用啊
      

  6.   

    不是吧?注释得不够详细吗?
    public class YourPanel extends JPanel implements CellColorInterface
    {
    ........
      void jbInit() throws Exception
      {
        .............
        table.setDefaultRenderer(String.class, new TableCellColorRenderer(this));
      }
      /**
       * 设置表格中每一格的内容。
       * @param row 格子所在的行
       * @param col 格子所在的列
       * @param isSelected 是否被选中
       * @return 格子希望的内容(String型)
       */
      public String getText(int row, int col, boolean isSelected)
      {
        String value = (String)this.tableModel.getValueAt(row, col);
        return value;
      }  /**
       * 设置表格中每一格的底色。
       * @param row 格子所在的行
       * @param col 格子所在的列
       * @param isSelected 是否被选中
       * @return 格子希望的颜色
       */
      public Color getColor(int row, int col, boolean isSelected)
      {
        Color color = this.table.getBackground();
        Color disableColor = this.getBackground();
        Color editDisAbleColor = Color.lightGray;
        /*if (!tableModel.isCellEditable(row, col))
             {
          color = editDisAbleColor;
             }*/
        if ( ( (Vector) rowVector.get(row)).get(table.getColumnCount()).equals("-1"))
        {
          if (col < 2)
          {
            color = disableColor;
          }
        }
        if (color != disableColor && isSelected)
        {
          color = table.getSelectionBackground();
        }
        return color;
      }}
      

  7.   

    我们的论坛提供Java GUI的资料还有我们自己的原创文章和代码,尽量避免转载他人的文章。欢迎提供技术资料。如果登陆不上,大概是cookie的问题。*************************************
    欢迎提供技术资料(原创、译文)
    http://www2.opensource.com.cn/
    *************************************
      

  8.   

    xxlcg(☆突突☆) 
    我的是一个frame 有一个table,我的frame已经implements implements ,但是就是不行啊,
      

  9.   

    报什么错?把frame的代码拈贴出来好吗?
      

  10.   

    我在Cell里面放的是String 不是Label , 上面的extends JLabel 可以吗?
    我还是些不出一个例子程序,
      

  11.   


    To  xxlcg(☆突突☆) :下面是我的部分代码,我想在init 里面把第 (1,1)(2,2) 单元格的背景色变成 red 我要怎么用你的class 啊?谢谢!!public class myframe
        extends JFrame { 
      
      javax.swing.table.DefaultTableModel tblModel = null;
      JTable jTable = null;  
      javax.swing.JScrollPane jsp1 = new JScrollPane();
      public ExchangeRateFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();     
         }catch (Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        contentPane = (JPanel)this.getContentPane();
        contentPane.setLayout(null);
        this.setSize(new Dimension(300, 530));
        this.setTitle("Exchange Rate Window");    
        jsp1.setBounds(new Rectangle(0, 0, this.getWidth() - 5, 250));  
        jTable=new JTable(5,5);
        this.jsp1.getViewport().add(jTable);    
        contentPane.add(jsp1, null);  
      }
      .......
      }
      

  12.   

    xxlcg(☆突突☆) 在吗??上面是我的代码啊,帮忙看看好吗?
      

  13.   

    public class myframe extends JFrame implements CellColorInterface{
    ...................
      private void jbInit() throws Exception {
        .........
        jTable.setDefaultRenderer(String.class, new TableCellColorRenderer(this));
      }
      public Color getColor(int row, int col, boolean isSelected)
      {
        if((row==1&&col==1)||(row==2&&col==2)){
          return Color.red;
        }
      }
      public String getText(int row, int col, boolean isSelected)
      {
        return (String)tblModel.getValueAt(row, col);
      }
    }
      

  14.   

    tblModel必须是jTable的模型。否则你就要get出jTable的模型来代替tblModel
      

  15.   

    在public Color getColor(int row, int col, boolean isSelected)的判断句后面再加上
    return jTable.getBackground();
    否则会编译不过^_^
      

  16.   

    嗯多谢  xxlcg(☆突突☆)  又学了不少东西了,你用java多少年了啊??再问一个问题如果我要用一个方法动态改变单元格的颜色我要怎么做啊难道要我每次 new 一个新
    的frame吗?请赐教!
      

  17.   

    使用接口CellColorInterface就是为了方便你设置表格格子颜色,你只要把你想要的颜色写在public Color getColor(int row, int col, boolean isSelected)里面,然后想改变的时候就jTable.updateUI();
      

  18.   

    to xxlcg(☆突突☆) :
       我用了你的办法,还是不会变色 (1,1)(2,2)单元格还是为白色,我是不是忘了要调用什么方法吗?还有
    jTable.setDefaultRenderer(String.class, new TableCellColorRenderer(this)); 这句设置了渲染后,在以后的程序并没有用到TableCellColorRenderer啊!能麻烦你说得清楚点吗?我刚开始学java ,周围的同学也不会,没人可以问,谢谢!
      

  19.   

    首先(1,1)(2,2)里面放的必须是String的值,否则TableCellColorRenderer是不会起作用的。
    new TableCellColorRenderer(this)看到参数“this”了吧?this就是当前类“myframe ”。
    你在看看TableCellColorRenderer里面有个属性cellColorInterface,其实最终cellColorInterface=myframe,可以理解为cellColorInterface引用了myframe实例,那么
    cellColorInterface.getColor(row,column,isSelected)就等价于
    myframe.getColor(row,column,isSelected),能看明白吗?
      

  20.   

    嗯多谢  xxlcg(☆突突☆) 也谢谢其他同志,揭帖了!!!