JTable的简单操作


JTable的简单操作:

  1. 填充数据:给单元格赋值或直接设置表格数据
  2. 增加一行空白行(手动输入添加数据)
  3. 直接增加一行数据
  4. 删除选中行的数据(删除一行,删除多行)
  5. 获取表格选中行数据(获取选中的一行或多行数据,并显示在文本域中)
  6. 设置JTable的某一列单元格编辑时为使用JComboBox(并设置双击单元格才启用JComboBox,默认单击使用)
public class TableDemo1 extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel tableModel;
	private JTextArea textArea;
	private String[] column=new String[] {"编号", "姓名", "职位", "领导", "入职", "工资", "提成", "部门"};

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TableDemo1 frame = new TableDemo1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TableDemo1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 450);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(60, 15, 465, 25);
		contentPane.add(panel);
		panel.setLayout(new GridLayout(0, 4, 50, 0));
		
		JButton setBtn = new JButton("赋值");
		setBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setData();
			}
		});
		panel.add(setBtn);
		
		JButton addBtn = new JButton("增加");
		addBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addData();
			}
		});
		panel.add(addBtn);
		
		JButton deleteBtn = new JButton("删除");
		deleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteData();
			}
		});
		panel.add(deleteBtn);
		
		JButton getBtn = new JButton("取值");
		getBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getData();
			}
		});
		panel.add(getBtn);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(60, 150, 468, 243);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null},
			},
			column
		));
		scrollPane.setViewportView(table);

	    tableModel=(DefaultTableModel) table.getModel();    //获得表格模型
//	    tableModel.setRowCount(0);    //清空表格中的数据
	    table.setModel(tableModel);
	    
	    textArea = new JTextArea();
	    JScrollPane scroll=new JScrollPane(textArea);
	    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	    scroll.setBounds(60, 56, 465, 76);
	    contentPane.add(scroll);
		
	    //设置JTable的某一列单元格编辑时使用JComboBox:
		String[] items = { "选项1", "选项2", "选项3", "选项4" };
        JComboBox editor = new JComboBox( items );
        DefaultCellEditor dce = new DefaultCellEditor(editor);
        dce.setClickCountToStart(2);//双击时才能使用JComboBox
        table.getColumnModel().getColumn(2).setCellEditor(dce);//设置在表格的第三列
        
        this.style();
	}
	
	//赋值(填充数据:给单元格赋值或使用二维数组直接设置表格数据)
	public void setData() {
		Object[][] datas=new Object[3][column.length];
		for(int i=0;i<3;i++) {
			for(int j=0;j<8;j++) {
				table.setValueAt(i+""+j,i, j);//给单元格赋值
				datas[i][j]=i+""+j;
			}
		}
//		table.setModel(new DefaultTableModel(datas,column));//使用二维数组设置表格数据
//		tableModel=(DefaultTableModel) table.getModel();//使用二维数组设置表格数据后更新tableModel
	}
	
	//增
	public void addData() {
		tableModel.setRowCount(tableModel.getRowCount()+1);//增加空行,手动编辑赋值
		//直接增加一行数据:
//			tableModel.addRow(new Object[] {123, "张三", "程序员", 7839, new Date(), 345.67, 234, 20});
		table.setModel(tableModel);
	}
	
	//删
	public void deleteData()
	{
		tableModel=(DefaultTableModel) table.getModel();
	    int[] selectedRows=table.getSelectedRows();
	    for(int i=0;i