JTable合并单元格

最近,我为了做一个管理系统,需要用到合并JTable的单元格。查找了很多资料,终于简单的实现了。现在把代码共享出来,希望对大家有用。

本程序主要实现行的合并,列的合并大家可以根据下面的代码修改。

CMap.java  :

package com;

public interface CMap {
 /**
  * @param row
  *            logical cell row
  * @param column
  *            logical cell column
  * @return number of columns spanned a cell
  */
 int span(int row, int column);

 /**
  * @param row
  *            logical cell row
  * @param column
  *            logical cell column
  * @return the index of a visible cell covering a logical cell
  */
 int visibleCell(int row, int column);
}

CTUI.java :

package com;

import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import javax.swing.*;

public class CTUI extends BasicTableUI {
 public void paint(Graphics g, JComponent c) {
  Rectangle r = g.getClipBounds();
//  int firstRow = table.rowAtPoint(new Point(0, r.y));
//  int lastRow = table.rowAtPoint(new Point(0, r.y + r.height));
  int firstCol = table.columnAtPoint( new Point( r.x , 0 ) );
  int lastCol = table.columnAtPoint(new Point( r.x + r.width, 0 ));
  // -1 is a flag that the ending point is outside the table
//  if (lastRow < 0)
//   lastRow = table.getRowCount() - 1;
  if (lastCol < 0)
   lastCol = table.g

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值