带有行标题的JTable

/**
 * @author chega
 * this class extends JTable and has two sub JTable class in it ,they are:RowHeadTable and LeftTopCornor,they are also JTable, 
 * RowHeadTable is the rowHeadView of JScrollPane ,and LeftTopCornor is the upper-top cornor of JScrollPane, you can use 
 * TableWithRowHead like this:<br/>
 * <b> 
 * 
 * JScrollPane pane = new JScrollPane(table);<br/><br/>
 * pane.setRowHeaderView(table.getRowHeadTable());<br/><br/>
 * pane.setCorner(JScrollPane.UPPER_LEFT_CORNER, table.getLeftTopCornor());<br/><br/>
 * </b>
 * If you select a row in RowHeadTable ,then the row at the same row index of TableWithRowHead will also be selectd,so vice-verse
 * if you select the only row in LeftTopCornor , the all rows in TableWithRowHead and RowHeadTable will be selected.
 * <br/>
 * for some reason , the LeftTopTable and RowHeadTable set selectionBackground to white, you can also set it to other new value, but 
 * some details should be considered.
 * <br/>
 * <br/>
 * <b>Attention:</b>
 * some method should not be invoked:table.setRowHeight() and table.getTableHeader().setHeight(),
 * alternative methods are supplied:setTableRowHeight() setTableHeadRowHeight().
 */
public class TableWithRowHead extends JTable{
	private static final long serialVersionUID = 1L;
	private RowHeadTable rowHead = null;
	private TableWithRowHead table = null;
	private LeftTopCornor leftTopCornor;
	
	public static void main(String...args){
		JFrame frame = new JFrame();
		final TableWithRowHead table = new TableWithRowHead(new DefaultTableModel(5,5));
		table.setAutoCreateRowSorter(true);
		table.hideColumn(0);
		JScrollPane pane = new JScrollPane(table);
		pane.setRowHeaderView(table.getRowHeadTable());
		pane.setCorner(JScrollPane.UPPER_LEFT_CORNER, table.getLeftTopCornor());
		frame.add(pane,BorderLayout.CENTER);
		JButton jb = new JButton("print selected rows");
		jb.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				print(table.getSelectedRows());
				print(table.getRealRows());
			}

			private void print(int[] selectedRows) {
				System.out.println("===================");
				for(int i=0;i<selectedRows.length;i++){
					System.out.println(selectedRows[i]+"\t");
				}
			}
		});
		frame.add(jb,BorderLayout.SOUTH);
		frame.setSize(500,400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public TableWithRowHead(Vector data, Vector columns){
		super(data,columns);
		this.init();
	}
	
	public TableWithRowHead(int row,int col){
		super(row,col);
		this.init();
	}
	
	public TableWithRowHead(DefaultTableModel model){
		super(model);
		this.init();
	}
	
	/**
	 * this method must be invoked in constructor
	 */
	private void init(){
		this.table = this;
		this.rowHead = new RowHeadTable();
		this.leftTopCornor = new LeftTopCornor();
		TableSelectionListener selectionListener = new TableSelectionListener();
		this.table.getSelectionModel().addListSelectionListener(selectionListener);
		TableMouseListener l = new TableMouseListener();
		this.rowHead.addMouseListener(l);
		this.leftTopCornor.addMouseListener(l);
	}
	
	public JTable getRowHeadTable(){
		return this.rowHead;
	}
	public JTable getLeftTopCornor(){
		return this.leftTopCornor;
	}
	
	/**
	 * make the row in RowHeadTable selected
	 * @param row
	 */
	public void setSelected(int row){
		this.rowHead.setValueAt(Boolean.TRUE, row, 0);
//		this.rowHead.addRowSelectionInterval(row, row);
	}
	/**
	 * 设置行选中(this method is for TableWithRowHead)
	 * @param rows
	 */
	public void setSelected(int[] rows){
		this.rowHead.clearSelected();
		this.rowHead.clearSelection();
		for(int i=0;i<rows.length;i++){
			this.setSelected(rows[i]);
		}
		//如果选中全部了,则leftTopCornor也选中,否则不选中
		if(rows.length==table.getRowCount()){
			this.leftTopCornor.setValueAt(Boolean.TRUE, 0, 0);
			this.leftTopCornor.selectAll();
		}else{
			this.leftTopCornor.setValueAt(Boolean.FALSE, 0, 0);
			this.leftTopCornor.clearSelection();
		}
	}
	/**
	 * 返回选中的行(this method is for TableWithRowHead)
	 */
	public int[] getRealRows(){
		int[] selectedRows = this.getSelectedRows();
		int[] realRows = new int[selectedRows.length];
		for(int i=0;i<realRows.length;i++){
			realRows[i] = this.convertRowIndexToModel(selectedRows[i]);
		}
		return realRows;
	}
	
	/**
	 * 隐藏某列
	 * @param col
	 */
	public void hideColumn(int col){
		JTableHeader tableHeader = this.getTableHeader();
		tableHeader.getColumnModel().getColumn(col).setPreferredWidth(0);
		tableHeader.getColumnModel().getColumn(col).setMaxWidth(0);
		tableHeader.getColumnModel().getColumn(col).setMinWidth(0);
		table.getColumnModel().getColumn(col).setPreferredWidth(0);
		table.getColumnModel().getColumn(col).setMaxWidth(0);
		table.getColumnModel().getColumn(col).setMinWidth(0);
	}
	/**
	 * set row height to a new value <b>height</b>
	 * @author chega
	 *
	 */
	public void setTableRowHeight(int height){
		this.table.setRowHeight(height);
		this.rowHead.setRowHeight(height);
	}
	/**
	 * 
	 * @param row
	 * @param height
	 */
	public void setTableRowHeight(int row,int height){
		this.table.setRowHeight(row,height);
		this.rowHead.setRowHeight(row, height);
	}
	/**
	 * set the table header height
	 */
	public void setTableHeadHeight(int height){
		this.table.getTableHeader().setPreferredSize(new Dimension(0,height));
		this.leftTopCornor.setRowHeight(height);
	}
	
	//==============innner class========================
	private class RowHeadTable extends JTable{
		public RowHeadTable(){
			super();
			this.setAlignmentX(SwingConstants.CENTER);
			this.setSelectionBackground(Color.white);
			DefaultTableModel model = new RowHeadTableModel(table.getRowCount(),1);
			this.setModel(model);
			this.getColumnModel().getColumn(0).setPreferredWidth(16);
			this.setPreferredScrollableViewportSize(new Dimension(16,0));
		}
		
		private void clearSelected(){
			for(int i=0;i<this.getRowCount();i++){
				this.setValueAt(Boolean.FALSE, i, 0);
			}
		}
	}
	
	
	private class LeftTopCornor extends JTable{
		public LeftTopCornor(){
			super();
			this.setToolTipText("全选");
			this.setAlignmentX(SwingConstants.CENTER);
			this.setSelectionBackground(Color.white);
			DefaultTableModel model = new RowHeadTableModel(1,1);
			this.setModel(model);
			this.setRowHeight(table.getTableHeader().getPreferredSize().height);
		}
	}
	
	
	private class RowHeadTableModel extends DefaultTableModel{
		public RowHeadTableModel(int row,int col){
			super(row,col);
		}
		@Override
		public boolean isCellEditable(int row,int col){
			return true;
		}
		@Override
		public Class<?> getColumnClass(int col){
			return Boolean.class;
		}
	}
	
	class TableSelectionListener implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if(e.getValueIsAdjusting()){
				return ;
			}
//			int[] selectedRows = table.getRealRows();
			int[] selectedRows = table.getSelectedRows();
			table.setSelected(selectedRows);
		}
	}
	
	class TableMouseListener extends MouseAdapter{
		@Override
		public void mouseReleased(MouseEvent e){
			if(e.getSource() instanceof RowHeadTable){
				int row = rowHead.rowAtPoint(new Point(e.getX(),e.getY()));
				if(row==-1){
					return ;
				}
				if((Boolean)rowHead.getValueAt(row, 0)){
					table.addRowSelectionInterval(row,row);
				}else{
					table.removeRowSelectionInterval(row, row);
					leftTopCornor.setValueAt(Boolean.FALSE, 0, 0);
					leftTopCornor.clearSelection();
				}
			}
			else if(e.getSource() instanceof LeftTopCornor){
				if((Boolean)leftTopCornor.getValueAt(0, 0)){//select all
					table.selectAll();
				}else{
					table.clearSelection();
					leftTopCornor.clearSelection();
					rowHead.clearSelection();
				}
			}
		}

	}
}

你可能感兴趣的:(jtable)