JTable 简单应用


package com.test.table.example1;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * 
 * @author LiuQing
 * @see 表格的操作
 * @version 1.0
 * @date 2010-8-11 下午2:54:52
 */
public class TableSelectionFrame extends JFrame {

	private static final long serialVersionUID = -3946729267999982017L;
	
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable(){

			@Override
			public void run() {
				try {
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
				JFrame frame = new TableSelectionFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
			
		});
	}
	
	public TableSelectionFrame() {
		setTitle("TableSelectionTest");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		model = new DefaultTableModel(10,10);
		//初始化表格模块数据
		for (int i = 0; i < model.getRowCount(); i++) {
			for (int j = 0; j < model.getColumnCount(); j++) {
				model.setValueAt((i+ 1) * (j + 1), i, j);
			}
		}
		
		table = new JTable(model);
		add(new JScrollPane(table));
		//定义删除列模型数据
		this.removedColumns = new ArrayList<TableColumn>();
		
		//定义菜条
		JMenuBar menuBar = new JMenuBar();
		//添加到窗体中
		this.setJMenuBar(menuBar);
		JMenu selectionMenu = new JMenu("Selection");
		menuBar.add(selectionMenu);
		//定义复选菜单
		final JCheckBoxMenuItem rowsItem = new JCheckBoxMenuItem("Rows");
		final JCheckBoxMenuItem columnsItem = new JCheckBoxMenuItem("Columns");
		final JCheckBoxMenuItem cellsItem = new JCheckBoxMenuItem("Cells");
		//根据表格属性设置UI显示值
		rowsItem.setSelected(table.getRowSelectionAllowed());
		columnsItem.setSelected(table.getColumnSelectionAllowed());
		cellsItem.setSelected(table.getCellSelectionEnabled()); 
		
		//对行模型添加监听器
		rowsItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				table.clearSelection();
				table.setRowSelectionAllowed(rowsItem.isSelected());
				columnsItem.setSelected(table.getColumnSelectionAllowed());
				cellsItem.setSelected(table.getCellSelectionEnabled()); 
			}
			
		});
		selectionMenu.add(rowsItem);
		
		columnsItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				table.clearSelection();
				table.setColumnSelectionAllowed(columnsItem.isSelected());
				
				rowsItem.setSelected(table.getRowSelectionAllowed());
				columnsItem.setSelected(table.getColumnSelectionAllowed());
				cellsItem.setSelected(table.getCellSelectionEnabled()); 
			}
			
		});
		selectionMenu.add(columnsItem);
		
		cellsItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				table.clearSelection();
				table.setCellSelectionEnabled(cellsItem.isSelected());
				
				rowsItem.setSelected(table.getRowSelectionAllowed());
				columnsItem.setSelected(table.getColumnSelectionAllowed());
				cellsItem.setSelected(table.getCellSelectionEnabled()); 
			}
			
		});
		selectionMenu.add(cellsItem);
		
		JMenu tableMenu = new JMenu("Edit");
		menuBar.add(tableMenu);
		
		JMenuItem hideColumnsItem = new JMenuItem("Hide Column");
		//添加隐藏操作
		hideColumnsItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				//通过表格获得先择的列
				int []selected = table.getSelectedColumns();
				//获得列模型
				TableColumnModel columnModel = table.getColumnModel();
				for (int i = selected.length - 1; i >= 0; i--) {
					//获得对应的列信息
					TableColumn column = columnModel.getColumn(selected[i]);
					table.removeColumn(column);
					removedColumns.add(column);
				}
			}
		});
		tableMenu.add(hideColumnsItem);
		
		JMenuItem showColumnsItem = new JMenuItem("Show Column");
		showColumnsItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				//显示被删除的列,从cache 中获得删除Column对象
				for (TableColumn tc:removedColumns) {
					table.addColumn(tc);
				}
				removedColumns.clear();
			}
		});
		tableMenu.add(showColumnsItem);
		
		JMenuItem addRowItem = new JMenuItem("Add Row");
		addRowItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				//新增行数据
				Integer newCells[] = new Integer[model.getColumnCount()];
				for (int i = 0; i < newCells.length; i++) {
					newCells[i] = (i + 1) * model.getRowCount() + 1;
				}
				model.addRow(newCells);
				
			}
			
		});
		tableMenu.add(addRowItem);
		
		JMenuItem removeRowsItem = new JMenuItem("Remove Row");
		removeRowsItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				//删除行数据
				int selected[] = table.getSelectedRows();
				for (int i = selected.length - 1; i >= 0; i--) {
					model.removeRow(selected[i]);
				}
			}
			
		});
		tableMenu.add(removeRowsItem);
		
		//清空数据
		JMenuItem clearCellsItem = new JMenuItem("Clear Cell");
		clearCellsItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < table.getRowCount(); i++) {
					for (int j = 0; j < table.getColumnCount(); j++) {
						if (table.isCellEditable(i, j)) {
							table.setValueAt(0, i,j);
						}
					}
				}
			}
			
		});
		tableMenu.add(clearCellsItem);
		
	}
	
	//定义表格模型
	private DefaultTableModel model;
	//表格
	private JTable table;
	//列模型
	private ArrayList<TableColumn> removedColumns;
	
	private static final int DEFAULT_WIDTH = 400;
	
	private static final int DEFAULT_HEIGHT = 300;

}
 

你可能感兴趣的:(jtable)