jtabel中加复选框的几种方法_第1页
jtabel中加复选框的几种方法_第2页
jtabel中加复选框的几种方法_第3页
jtabel中加复选框的几种方法_第4页
jtabel中加复选框的几种方法_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一种:三个类:CheckBoxRenderer.java CheckButtonEditor.java table.javaCheckBoxRenderer.javaimport java.awt.Component;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.JCheckBox;import javax.swing.JLabel;import javax.swing.JTable;import javax.swing.table.TableCellRenderer;class CheckBoxRenderer implements TableCellRendererpublic Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) if (value=null) return null;return (Component)value;CheckButtonEditor.javaimport java.awt.Component;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.DefaultCellEditor;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JRadioButton;import javax.swing.JTable;import javax.swing.JTextField;class CheckButtonEditor extends DefaultCellEditorimplements ItemListener private JCheckBox button;public CheckButtonEditor(JCheckBox checkBox) super(checkBox);public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) if (value=null) return null;button = (JCheckBox)value;button.addItemListener(this);return (Component)value;public Object getCellEditorValue() button.removeItemListener(this);return button;public void itemStateChanged(ItemEvent e) super.fireEditingStopped();table.javaimport java.awt.Dimension;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.event.TableModelEvent;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellRenderer;public class table implements MouseListenerJTable table=new JTable();public table()JFrame frame=new JFrame(sjh);frame.setLayout(null);table=this.gettable();table.addMouseListener(this);JScrollPane src=new JScrollPane(table);src.setBounds(0, 0, 400, 200);frame.setSize(new Dimension(400,200);frame.add(src);frame.setVisible(true);public JTable gettable()DefaultTableModel dm = new DefaultTableModel(); dm.setDataVector( new Objectnew JCheckBox(111),new JCheckBox(111),new JCheckBox(111),new JCheckBox(111),new JCheckBox(111),new JCheckBox(111), new JCheckBox(222),new JCheckBox(222),new JCheckBox(222),new JCheckBox(222),new JCheckBox(222),new JCheckBox(222), new JCheckBox(333),new JCheckBox(333),new JCheckBox(333),new JCheckBox(333),new JCheckBox(333),new JCheckBox(333), , new Object选择,结果物,说明,类型,发包要求文档,操作); JTable table = new JTable(dm) public void tableChanged(TableModelEvent e) super.tableChanged(e); repaint(); ; table.getColumn(选择).setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(结果物).setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(说明).setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(类型).setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(发包要求文档).setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(操作).setCellEditor(new CheckButtonEditor(new JCheckBox ();/ table.getColumn(选择).setCellRenderer(new RadioButtonRenderer();/ table.getColumn(选择).setCellRenderer(new CheckBoxRenderer(); / table.setCellEditor(new CheckButtonEditor(new JCheckBox (); table.getColumn(选择).setCellRenderer(new CheckBoxRenderer(); table.getColumn(结果物).setCellRenderer(new CheckBoxRenderer(); table.getColumn(说明).setCellRenderer(new CheckBoxRenderer(); table.getColumn(类型).setCellRenderer(new CheckBoxRenderer(); table.getColumn(发包要求文档).setCellRenderer(new CheckBoxRenderer(); table.getColumn(操作).setCellRenderer(new CheckBoxRenderer(); return table;public static void main(String args)new table();public void mouseClicked(MouseEvent arg0) / TODO Auto-generated method stubpublic void mouseEntered(MouseEvent arg0) / TODO Auto-generated method stubpublic void mouseExited(MouseEvent arg0) / TODO Auto-generated method stubpublic void mousePressed(MouseEvent arg0) / TODO Auto-generated method stubpublic void mouseReleased(MouseEvent arg0) / TODO Auto-generated method stub第二种: AbstractTableModelTest.javaimport javax.swing.JFrame;import javax.swing.JComboBox;import javax.swing.JCheckBox;import javax.swing.JTextField;import java.awt.Container;import javax.swing.table.AbstractTableModel;import javax.swing.JTable;import javax.swing.JScrollPane;import javax.swing.DefaultCellEditor;import javax.swing.table.TableColumn;public class AbstractTableModelTest extends JFrame private JTable table=null; private JScrollPane jsp=null; private JComboBox cob=null; private JCheckBox ckb=null; private JTextField txt=null; public AbstractTableModelTest() this.setTitle(JTable的ListSelectionModeld的监听事件); this.setSize(400,360); /获取窗体的内容面板 Container container=this.getContentPane(); MyAbstractTableModel myModel=new MyAbstractTableModel(); /JTable table=new JTable(myModel); /获得表格的表格列类 TableColumn tc1=table.getColumnModel().getColumn(2); TableColumn tc2=table.getColumnModel().getColumn(4); TableColumn tc3=table.getColumnModel().getColumn(5); /实例化JCheckBox ckb=new JCheckBox(); tc1.setCellEditor(new DefaultCellEditor(ckb); /实例化JComboBox cob=new JComboBox(); cob.addItem(HN); cob.addItem(HB); cob.addItem(BJ); tc2.setCellEditor(new DefaultCellEditor(cob); /实例化JTextField txt=new JTextField(); txt.setSize(100,26); tc3.setCellEditor(new DefaultCellEditor(txt); /JScrollPane jsp=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); container.add(jsp); /获得自定义的抽象表格模型 this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); public static void main(String args) new AbstractTableModelTest(); class MyAbstractTableModel extends AbstractTableModel /定义表头数据 String head=ID,NAME,SEX(boy),AGE,ADDRESS,DEMO; /创建类型数组 / Class typeArray=Object.class,Object.class,Boolean.class,int.class,Object.class,Object.class; /定义表的内容数据 Object data1=200913420125,SUMMER,new Boolean(true),new Integer(20),1,NULL; Object data2=200913420124,WULEI,new Boolean(true),new Integer(20),2,NULL; Object data3=200913420125,BOOK,new Boolean(false),new Integer(20),3,NULL; Object data4=200913420125,CUP,new Boolean(true),new Integer(20),4,NULL; Object data5=200913420125,MOUSE,new Boolean(true),new Integer(20),5,NULL;/定义表格每一列的数据类型 Class typeArray=Object.class,Object.class,Boolean.class,Integer.class,JComboBox.class,Object.class; Object data=data1,data2,data3,data4,data5; /获得表格的列数 public int getColumnCount() return head.length; /获得表格的行数public int getRowCount(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论