TablecellRenderer和TableCellEditor

TableCellRenderer就是用来绘制展示当前cell单元数值内容的,你可以用文字、数值或者图片来表示内容,当然最强大展示方式的就是通过自定义自己的renderer组件,通过Java2D来绘制各种天花乱坠的效果。


TableCellEditor主要是用来当用户点击在具体cell时进行编辑的组件,所以TableCellEditor除了具有TableCellRenderer一样的绘制功能外还可以进行交互动作,例如在cell上出现下拉框、勾选框甚至通过按钮弹出更复杂的对话框让用户进行输入编辑。

 

他们都是JPanel的子类

public Component getTableCellEditorComponent(JTable arg0, Object arg1,
   boolean arg2, int arg3, int arg4)
 {
  // TODO Auto-generated method stub
  return null;
 }

TableCellEditor主要方法  this

如果添加JButton  都是在这个基础的JComponent上面添加的组件

 

TableCellRenderer也是类似

------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
//主类
//@SuppressWarnings("unchecked")
public class TryJTable extends JFrame
{
    private static final long serialVersionUID = 1L;
    //创建自定义表格模型对象
    MyTableModel dtm=new MyTableModel();
    //创建JTable对象
    JTable jt=new JTable(dtm);
    //将JTable封装进滚动窗格
    JScrollPane jsp=new JScrollPane(jt);   
    //自定义的表格模型
    private class MyTableModel extends AbstractTableModel
    {

        private static final long serialVersionUID = 1L;
        //创建表示各个列类型的类型数组
        Class[] typeArray=
              {Object.class,Double.class,Boolean.class,Icon.class,Color.class};
        //创建列标题字符串数组
        String[] head={"Object","Number","Boolean","Icon","Color"};
        //创建初始表格数据
        Object[][] data={{"这里是字符串1",new Integer(20),new Boolean(true),
                            new ImageIcon("D:/p1.gif "),Color.black},
                        {"这里是字符串2",new Double(12.34),new Boolean(false),
                        new ImageIcon("D:/p2.gif "),Color.darkGray},
                        {"这里是字符串3",new Integer(125),new Boolean(true),
                        new ImageIcon("D:/p3.gif"),Color.white}};
        //重写getColumnCount方法
        public int getColumnCount()
        {
            return head.length;
        }
        //重写getRowCount方法   
        public int getRowCount()
        {
            return data.length;
        }
        //重写getColumnName方法
        public String getColumnName(int col)
        {
            return head[col];
        }
        //重写getValueAt方法
        public Object getValueAt(int r,int c)
        {
            return data[r][c];
        }
        //重写getColumnClass方法
        public Class getColumnClass(int c)
        {
            return typeArray[c];
        }
        //重写isCellEditable方法
        public boolean isCellEditable(int r,int c)
        {
            return true;
        }
        //重写setValueAt方法
        public void setValueAt(Object value,int r,int c)
        {
            data[r][c]=value;
            //
            this.fireTableCellUpdated(r,c);
        }
    }
    //自定义的绘制器
    private class MyCellRendererForColorClass
                    extends JLabel implements TableCellRenderer
    {
        private static final long serialVersionUID = 1L;
        //定义构造器
        public MyCellRendererForColorClass ()
        {
            //设置标签为不透明状态
            this.setOpaque(true);
            //设置标签的文本对齐方式为居中
            this.setHorizontalAlignment(JLabel.CENTER);
        }
        //实现获取呈现控件的getTableCellRendererComponent方法
        public Component getTableCellRendererComponent(JTable table,Object value,
                           boolean isSelected,boolean hasFocus,int row,int column)
        {           
            //获取要呈现的颜色
            Color c=(Color)value;
            //根据参数value设置背景色
            this.setBackground(c);
            //设置前景色为背景色反色
            this.setForeground(new Color(255-c.getRed()
                                ,255-c.getGreen(),255-c.getBlue()));
            //设置标签中显示RGB的值
            this.setText("["+c.getRed()+","+c.getGreen()+","+c.getBlue()+"]");
            //将自己返回
            return this;   
        }
    }   
    //声明Color类型的自定义编辑器
    class MyEditorForColorClass extends AbstractCellEditor
            implements TableCellEditor,ActionListener
    {
        private static final long serialVersionUID = 1L;
        //定义Color变量
        Color c;
        //定义对话框变量
        JDialog jd;
        //创建一个按扭
        JButton jb=new JButton();
        //创建JColorChooser对象
        JColorChooser jcc=new JColorChooser();
        //声明一个常量
        public static final String EDIT="edit";
        //定义构造器
        public MyEditorForColorClass()
        {
            //为按扭注册监听器
            jb.addActionListener(this);
            //设置此按扭的动作命令
            jb.setActionCommand(EDIT);
            //获取颜色选择器
            jd=JColorChooser.createDialog(jb,"选择颜色",true,jcc,this,null);
        }
        //实现actionPerformed方法
        public void actionPerformed(ActionEvent e)
        {
            //测试获得的动作命令是否等于EDIT常量
            if(e.getActionCommand().equals(EDIT))
            {
                //设置按扭的背景颜色
                jb.setBackground(c);
                //设置前景色为背景色反色
                jb.setForeground(new Color(255-c.getRed()
                                ,255-c.getGreen(),255-c.getBlue()));
                //设置按钮中显示RGB的值
                jb.setText("["+c.getRed()+","+c.getGreen()+","+c.getBlue()+"]");               

                //设置颜色选择器的颜色
                jcc.setColor(c);
                //设置颜色选择器可见
                jd.setVisible(true);
                //通知所有监听器,以延迟方式创建事件对象
                this.fireEditingStopped();           
            }
            else
            {
                //获取颜色
                c=jcc.getColor();
            }
        }
        //定义getCellEditorValue方法返回颜色值
        public Object getCellEditorValue()
        {
            return c;
        }
        //重写getTableCellEditorComponent方法
        public Component getTableCellEditorComponent(JTable table,
            Object value,boolean isSelected,int row,int column)
        {
            c=(Color)value;
            return jb;     
        }
    }
    //声明Icon类型的自定义编辑器
    class MyEditorForIconClass extends AbstractCellEditor
            implements TableCellEditor,ActionListener
    {
        private static final long serialVersionUID = 1L;
        //定义Icon变量
        Icon icon;
        //创建一个按扭
        JButton jb=new JButton();
        //创建JColorChooser对象
        JFileChooser jfc=new JFileChooser();
        //声明一个常量
        public static final String EDIT="edit";
        //定义构造器
        public MyEditorForIconClass()
        {
            //为按扭注册监听器
            jb.addActionListener(this);
            //设置此按扭的动作命令
            jb.setActionCommand(EDIT);
        }
        //实现actionPerformed方法
        public void actionPerformed(ActionEvent e)
        {
            //测试获得的动作命令是否等于EDIT常量
            if(e.getActionCommand().equals(EDIT))
            {
                //设置按扭的图标
                jb.setIcon(icon);
                //显示文件选择器对话框
                jfc.showOpenDialog(jb);
                //获取新图片
                if(jfc.getSelectedFile()!=null)
                {
                    icon=new ImageIcon(jfc.getSelectedFile().getAbsolutePath());
                }               
                //通知所有监听器,以延迟方式创建事件对象
                this.fireEditingStopped();           
            }
        }
        //定义getCellEditorValue方法返回图标
        public Object getCellEditorValue()
        {
            return icon;
        }
        //重写getTableCellEditorComponent方法
        public Component getTableCellEditorComponent(JTable table,
            Object value,boolean isSelected,int row,int column)
        {
            icon=(Icon)value;
            return jb;     
        }
    }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wzh32/archive/2009/05/01/4141543.aspx

-----------------------------------------------------

import java.awt.*;  
 
import javax.swing.*;  
import javax.swing.table.*;  
 
public class RoundColorTable extends JFrame

 {  
     private String[] colname = {"第1列","第2列","第3列","第4列","第5列"}; //表头信息  
     private String[][] data = new String[10][5]; //表内容   
     private JTable table;  
     public RoundColorTable() {  
         //表内容赋值  
         for(int i = 0; i < 10; i++) {  
             for(int j = 0; j < 5; j++) {  
                 data[i][j] = "( " + (j+1) + ", " + (i+1) + " )";  
             }  
         }  
         table = new JTable(new DefaultTableModel(data,colname));  
         TableCellRenderer tcr = new ColorTableCellRenderer();  
         table.setDefaultRenderer(Object.class,tcr);//为JTable增加渲染器,因为是针对于表格中的所有单元格,所有用Object.class  
         add(new JScrollPane(table),BorderLayout.CENTER);  
         setVisible(true);  
         setSize(500,300);  
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     }  
     public static void main(String args[]) {  
         new RoundColorTable();  
     }  
}  
 
class ColorTableCellRenderer extends DefaultTableCellRenderer  
{  
    DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();  
    public Component getTableCellRendererComponent(JTable table, Object value,  
        boolean isSelected, boolean hasFocus, int row, int column) {  
        if(row%2 == 0){  
            //调用基类方法  
            return super.getTableCellRendererComponent(table, value, isSelected,hasFocus, row, column);  
        }  
        else{  
            return renderer.getTableCellRendererComponent(table, value, isSelected,hasFocus, row, column);  
        }  
    }  
    //该类继承与JLabel,Graphics用于绘制单元格,绘制红线  
    public void paintComponent(Graphics g){  
        super.paintComponent(g);  
        Graphics2D g2=(Graphics2D)g;  
        final BasicStroke stroke=new BasicStroke(2.0f);  
        g2.setColor(Color.RED);  
        g2.setStroke(stroke);  
        g2.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);  
    }  
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chosen0ne/archive/2009/08/16/4453267.aspx

你可能感兴趣的:(软甲编程)