java jtable 单元格合并_JTable 单元格合并 【转】

本文介绍如何在Java中实现JTable的单元格合并,提供了CMap接口、CTUI类、CTable类和CMap1类的详细代码示例,以帮助开发者在管理系统的界面设计中实现表格单元格的合并功能。
摘要由CSDN通过智能技术生成

最近,我为了做一个管理系统,需要用到合并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));//JTable table=super.table就好理解

// 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.getColumnCount() - 1;for (int i = firstCol; i <= lastCol; i++)

paintCol(i, g);

}

private void paintCol(int col, Graphics g) {

Rectangle r = g.getClipBounds();

for (int i = 0; i < table.getRowCount(); i++) {

Rectangle r1 = table.getCellRect( i, col, true);

if (r1.intersects(r)) // at least a part is visible{

int sk = ((CTable) table).map.visibleCell( i, col );

paintCell( sk, col, g, r1);// increment the column counter

i += ((CTable) table).map.span( sk, col ) - 1;      }

}

}

private void paintCell(int row, int column, Graphics g, Rectangle area) {

int verticalMargin = table.getRowMargin();

int horizontalMargin = table.getColumnModel().getColumnMargin();

Color c = g.getColor();

g.setColor(table.getGridColor());

g.drawRect(area.x, area.y, area.width - 1, area.height - 1);

g.setColor(c);

area.setBounds(area.x + horizontalMargin / 2, area.y + verticalMargin/ 2,

area.width - horizontalMargin,

area.height- verticalMargin);

if (table.isEditing() && table.getEditingRow() == row&& table.getEditingColumn() == column) {

Component component = table.getEditorComponent();

component.setBounds(area);

component.validate();

} else {

TableCellRenderer renderer = table.getCellRenderer(row, column);

Component component = table.prepareRenderer(renderer, row, column);

if (component.getParent() == null)

rendererPane.add(component);

rendererPane.paintComponent(g, component, table, area.x, area.y, area.width, area.height, true);

}

}

}

CTable.java :

package com;

import javax.swing.*;

import javax.swing.table.*;

import java.awt.*;

public class CTable extends JTable {

public CMap map;

public CTable(CMap cmp, TableModel tbl) {

super(tbl);

map = cmp;

setUI(new CTUI());

}

public Rectangle getCellRect(int row, int column, boolean includeSpacing) {

// required because getCellRect is used in JTable constructor

if (map == null)

return super.getCellRect(row, column, includeSpacing);

// add widths of all spanned logical cells

int sk = map.visibleCell(row, column);

//Rectangle r1 = super.getCellRect(row, sk, includeSpacing);

Rectangle r1 = super.getCellRect( sk, column, includeSpacing);

if (map.span( sk, column ) != 1)

for (int i = 1; i < map.span( sk, column ); i++) {

//r1.width += getColumnModel().getColumn(sk + i).getWidth();

r1.height += this.getRowHeight( sk + i );

}

return r1;

}

public introwAtPoint(Point p) {

int x = super.columnAtPoint(p);

// -1 is returned by columnAtPoint if the point is not in the table

if (x < 0)

return x;

int y = super.rowAtPoint(p);

return map.visibleCell(y, x);

}

}

CMap1.java :

/******************************************************************************************

CMap1对CMap地实现

span( ) 表示合并的单元格的列,返回的是合并的格数。

visibleCell() 表示要渲染的格。返回的渲染的开始格的行。

本程序的table是16行10列,合并的单元格是第一列和最后一列(最后一列是第10列)每两个行。

*******************************************************************************************/

package com;

import javax.swing.*;

import javax.swing.table.*;

class CMap1 implements CMap {

public int span(int row, int column) {

if( column == 0 || column == 9 )

return 2;

return 1;

}

public int visibleCell(int row, int column) {

if( ( ( row >= 0 ) && ( row < 2 ) ) && ( column == 0 || column == 9 ) )

return 0;

if( ( ( row >= 2 ) && ( row < 4 ) ) && ( column == 0 || column == 9 ) )

return 2;

if( ( ( row >= 4 ) && ( row < 6 ) ) && ( column == 0 || column == 9 ) )

return 4;

if( ( ( row >= 6 ) && ( row < 8 ) ) && ( column == 0 || column == 9 ) )

return 6;

if( ( ( row >= 8 ) && ( row < 10 ) ) && ( column == 0 || column == 9 ) )

return 8;

if( ( ( row >= 10 ) && ( row < 12 ) ) && ( column == 0 || column == 9 ) )

return 10;

if( ( ( row >= 12 ) && ( row < 14 ) ) && ( column == 0 || column == 9 ) )

return 12;

if( ( ( row >= 14 ) && ( row < 16 ) ) && ( column == 0 || column == 9 ) )

return 14;

System.out.println( ">>>row = " + row + "column = " + column );

return row;

}

}

下面的程序进行测试。

package com;

import javax.swing.*;

import javax.swing.table.*;

public class CTest {

public static void main(String args[]) {

JFrame jf = new JFrame("Table with cell spanning");

CMap m = new CMap1();

DefaultTableModel tm = new DefaultTableModel( 16, 10 ){

public boolean isCellEditable( int indexRow, int indexColumn )

{return false;}

};

//tm.isCellEditable( 16, 10 );

tm.setValueAt( "port1", 0, 0);//对一个合并的单元格填一个数据。

jf.getContentPane().add(new JScrollPane(new CTable(m, tm)));

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

jf.setSize(500, 500);

jf.show();//jf.setVisibale(true)

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要更改Swing JTable单元格颜色,您可以使用JTable上的TableCellRenderer接口。这个接口允许您自定义单元格的呈现方式,包括单元格的背景和前景颜色。 下面是一个简单的例子,演示如何更改JTable单元格的颜色: ``` import java.awt.Color; import java.awt.Component; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class JTableColorExample extends JFrame { public JTableColorExample() { // 创建表格模型和表格 DefaultTableModel model = new DefaultTableModel(new Object[][]{ {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4} }, new Object[]{"Name", "Value"}); JTable table = new JTable(model); // 创建单元格渲染器 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // 调用父类的方法获取默认的单元格渲染组件 Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // 如果值为奇数,设置背景为红色,否则为绿色 if ((Integer) value % 2 == 1) { c.setBackground(Color.RED); } else { c.setBackground(Color.GREEN); } return c; } }; // 设置单元格渲染器 table.setDefaultRenderer(Object.class, renderer); // 将表格添加到窗口中 add(new JScrollPane(table)); // 设置窗口属性并显示 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JTableColorExample(); } } ``` 在这个例子中,我们创建了一个JTable,其中包含两列数据:名称和值。然后,我们创建了一个DefaultTableCellRenderer的子类,重写了getTableCellRendererComponent方法来更改单元格的背景颜色,使奇数单元格为红色,偶数单元格为绿色。最后,我们将此单元格渲染器设置为表格的默认单元格渲染器。 运行这个例子,您应该看到一个带有不同颜色单元格JTable
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值