利用swing的渲染器实现表套表功能

package com.leagsoft.table;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class TableOfTable extends JTable{
	public TableOfTable() {
		//添加一个默认的Model,根据自己的要求定制
		this.setModel(new DefaultTableModel(5,5));
		//将第二行的高度设宽一些
		this.setRowHeight(1, rowHeight*4);
	}
	//重载getCellRenderer 
	@Override
	public TableCellRenderer getCellRenderer(int row, int column) {
		if (row==1 && column==4) {
			return new TableCellRenderer(){
				JTable subTable = new JTable(new DefaultTableModel(4,4));
				@Override
				public Component getTableCellRendererComponent(JTable table,
						Object value, boolean isSelected, boolean hasFocus,
						int row, int column) {
					return subTable;
				}
				
			};
		} else {
			return super.getCellRenderer(row, column);
		}
		
	}
	
	
}

你可能感兴趣的:(swing)