日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jtable 单元格合并_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));//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)

}

}

總結

以上是生活随笔為你收集整理的java jtable 单元格合并_JTable 单元格合并 【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。