jtable超复杂表头

haoyaning2006 2006-12-25 09:56:29
---------------------------
| abc | ss | s | dhc |
|--------------------------
| aaaabbbb | ss |
|-------------------| |
| | ss | s | nn |
| |-------------| |
| abc | ss | s | b |
---------------------------
| abc | ss | s | b |
---------------------------

第一行和第二行的难实现,
---------------------
| abc | ss | s |
|--------------------
| aaaabbbb |
|-------------------|
还要注意有的单元格没有边框,和字休位置

...全文
671 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
emin_lee 2007-03-28
  • 打赏
  • 举报
回复
mark!
Ji秋风 2007-03-28
  • 打赏
  • 举报
回复
收藏!
qzjackie 2007-03-28
  • 打赏
  • 举报
回复
:gtlang78() ( 两星(中级))
hengxxh() ( 二级(初级)
..............................
的方法,不错哦。
你可以 试试哦。
hengxxh 2007-03-18
  • 打赏
  • 举报
回复
for (Component cellComp : cellComps) {
HeaderCellConstraints cell = compCellMap.get(cellComp);
if (cell == null) {
continue;
}

int cellX = 0;
for (int i = 0; i < cell.x; i++) {
cellX += columnModel.getColumn(i).getWidth();
}

int cellWid = 0;
for (int i = 0; i < cell.colSpan; i++) {
cellWid += columnModel.getColumn(cell.x + i).getWidth();
}

int cellY = rowHeight * cell.y;
int cellHei = rowHeight * cell.rowSpan;

cellComp.setBounds(cellX, cellY, cellWid, cellHei);
}

这种 FOR 循环的使用 小弟有点不解,能否解释一下,谢谢!!
hengxxh 2007-03-16
  • 打赏
  • 举报
回复
能否稍 详解 一下,不甚感激!
beibeiG 2007-03-16
  • 打赏
  • 举报
回复
变态的客户
hengxxh 2007-03-13
  • 打赏
  • 举报
回复
而且 我在 JDK1.6里显示语法错误,请高手指点!
hengxxh 2007-03-13
  • 打赏
  • 举报
回复
for (Component cellComp : cellComps) {
HeaderCellConstraints cell = compCellMap.get(cellComp);
if (cell == null) {
continue;
}

int cellX = 0;
for (int i = 0; i < cell.x; i++) {
cellX += columnModel.getColumn(i).getWidth();
}

int cellWid = 0;
for (int i = 0; i < cell.colSpan; i++) {
cellWid += columnModel.getColumn(cell.x + i).getWidth();
}

int cellY = rowHeight * cell.y;
int cellHei = rowHeight * cell.rowSpan;

cellComp.setBounds(cellX, cellY, cellWid, cellHei);
}

这种 FOR 循环的使用 小弟有点不解,能否解释一下,谢谢?
shan1119 2007-03-13
  • 打赏
  • 举报
回复
for (Component cellComp : cellComps) {
------------------------------------------
跟vb里的for each一样,跟iterator类似吧。
whusj 2007-03-09
  • 打赏
  • 举报
回复
顶一下!
haoyaning2006 2006-12-26
  • 打赏
  • 举报
回复
我也不想做这样复杂的表头,没办法,客户要求。

试试 http://www.finereport.com 可以的
你的东西我下载了,没用懂
windproof 2006-12-26
  • 打赏
  • 举报
回复
gtlang78() 大哥果然超强!!敬礼!!学习!!!!
windproof 2006-12-26
  • 打赏
  • 举报
回复
说实话!这样的报表我以前做过!不过我是用Drawline一条条画的!哈哈实在没办法了!不过看上去还不错!能用!帮你顶起来!
gtlang78 2006-12-26
  • 打赏
  • 举报
回复
//有点儿乱,凑合看吧, 另外还有个缺点就是没法儿用鼠标拖动列和调整列宽。

import java.awt.*;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;


public class T
{
public static void main(String[] args) throws Exception
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

JTable table = new JTable(20, 4) {
@Override
protected JTableHeader createDefaultTableHeader()
{
Header header = new Header(20);
header.add(new HeaderCellLabel("abc", 0, 0, 1, 1), new HeaderCellConstraints(0, 0));
header.add(new HeaderCellLabel("ss", 0, 0, 1, 1), new HeaderCellConstraints(1, 0));
header.add(new HeaderCellLabel("s", 0, 0, 1, 1), new HeaderCellConstraints(2, 0));
header.add(new HeaderCellLabel("dhc", 0, 0, 1, 1), new HeaderCellConstraints(3, 0));
header.add(new HeaderCellLabel("aaaabbbb", 0, 0, 1, 1), new HeaderCellConstraints(0, 1, 3));
header.add(new HeaderCellLabel("abc", 0, 0, 1, 1), new HeaderCellConstraints(0, 2, 1, 2));
header.add(new HeaderCellLabel("ss", 0, 0, 1, 1), new HeaderCellConstraints(1, 2));
header.add(new HeaderCellLabel("ss", 0, 0, 1, 1), new HeaderCellConstraints(1, 3));
header.add(new HeaderCellLabel("s", 0, 0, 1, 1), new HeaderCellConstraints(2, 2));
header.add(new HeaderCellLabel("s", 0, 0, 1, 1), new HeaderCellConstraints(2, 3));
header.add(new HeaderCellLabel("<html>ss<p>nn<p>b</html>", 0, 0, 1, 1), new HeaderCellConstraints(3, 1, 1, 3));

return header;
}
};
JScrollPane sp = new JScrollPane(table);
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
p.add(sp);

JFrame f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

class Header extends JTableHeader
{
private int rowHeight = 20;

public Header(int rowHeight)
{
setLayout(new HeaderLayout());

this.rowHeight = rowHeight;
}

@Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
HeaderLayout layout = (HeaderLayout) getLayout();
d.height = layout.getRowCount() * rowHeight;

return d;
}
}

class HeaderCellLabel extends JLabel
{

public HeaderCellLabel(Icon icon, int top, int left, int bottom, int right)
{
this("", icon, top, left, bottom, right);
}

public HeaderCellLabel(String text, Icon icon, int top, int left, int bottom, int right)
{
super(text, icon, JLabel.CENTER);

setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, Color.gray));
}

public HeaderCellLabel(String text, int top, int left, int bottom, int right)
{
this(text, null, top, left, bottom, right);
}
}

class HeaderCellConstraints {
public final int x;
public final int y;
public final int colSpan;
public final int rowSpan;

public HeaderCellConstraints(final int x, final int y, final int colSpan, final int rowSpan)
{
this.x = x;
this.y = y;
this.colSpan = colSpan;
this.rowSpan = rowSpan;
}

public HeaderCellConstraints(final int x, final int y, final int colSpan)
{
this(x, y, colSpan, 1);
}

public HeaderCellConstraints(final int x, final int y)
{
this(x, y, 1, 1);
}
}

class HeaderLayout implements LayoutManager2 {
private HashMap<Component, HeaderCellConstraints> compCellMap =
new HashMap<Component, HeaderCellConstraints>();

private int rowCount = 0;

public void addLayoutComponent(Component comp, Object constraints)
{
if (constraints == null || !(constraints instanceof HeaderCellConstraints)) {
return;
}

HeaderCellConstraints cell = (HeaderCellConstraints) constraints;
compCellMap.put(comp, cell);
rowCount = Math.max(rowCount, cell.y + cell.rowSpan);
}

public int getRowCount()
{
return rowCount;
}

public float getLayoutAlignmentX(Container target)
{
return 0;
}

public float getLayoutAlignmentY(Container target)
{
return 0;
}

public void invalidateLayout(Container target)
{
}

public Dimension maximumLayoutSize(Container target)
{
return preferredLayoutSize(target);
}

public void addLayoutComponent(String name, Component comp)
{
throw new IllegalArgumentException();
}

public void layoutContainer(Container parent)
{
JTableHeader header = (JTableHeader) parent;
JTable table = header.getTable();

TableColumnModel columnModel = table.getColumnModel();
int rowHeight = header.getHeight() / rowCount;

Component[] cellComps = parent.getComponents();
for (Component cellComp : cellComps) {
HeaderCellConstraints cell = compCellMap.get(cellComp);
if (cell == null) {
continue;
}

int cellX = 0;
for (int i = 0; i < cell.x; i++) {
cellX += columnModel.getColumn(i).getWidth();
}

int cellWid = 0;
for (int i = 0; i < cell.colSpan; i++) {
cellWid += columnModel.getColumn(cell.x + i).getWidth();
}

int cellY = rowHeight * cell.y;
int cellHei = rowHeight * cell.rowSpan;

cellComp.setBounds(cellX, cellY, cellWid, cellHei);
}
}

public Dimension minimumLayoutSize(Container parent)
{
return preferredLayoutSize(parent);
}

public Dimension preferredLayoutSize(Container parent)
{
return new Dimension(1,1);
}

public void removeLayoutComponent(Component comp)
{
compCellMap.remove(comp);
rowCount = 0;

for (HeaderCellConstraints cell : compCellMap.values()) {
rowCount = Math.max(rowCount, cell.y + cell.rowSpan);
}
}
}
carlkkx 2006-12-26
  • 打赏
  • 举报
回复
自己写一个tableheader的UI吧
shengli_liao 2006-12-26
  • 打赏
  • 举报
回复
学习中
lixiaoxue85 2006-12-25
  • 打赏
  • 举报
回复
自虐啊..

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧