中国Java开发网 中国Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» 中国Java开发网 » Java GUI 设计 » Swing  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
话题被移动
该话题已被移动 - floater , 2004-10-18 11:14
如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。
作者 如何用JTable设置表头多行 [精华]
kylin





发贴: 61
积分: 0
于 2004-10-18 10:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
例如下图
不知道该如何出现象下面的表头,跪谢各位大虾指点,不胜感激

——————————————————————————
职业 | 日期 | 代码 |
--------------------------| | |
医生 | 律师 | | |
————————————————————|——————|


kavinwang edited on 2004-10-28 14:35

作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-18 10:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,刚才贴错了,现在重贴

表头.bmp (576.05k)




作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-18 11:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,又来一次



作者 Re:如何用JTable设置表头多行 [Re:kylin]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-10-18 11:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://www.physci.org/codes/tame/index.jsp

get Nobuo's zip file, it has more than you need.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:如何用JTable设置表头多行 [Re:kylin]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-10-18 11:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
don't cross post!!!


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-18 15:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我下载了,运行了,但是“MultiWidthHeaderExample”这个类运行出错,我看了半天,没看出什么头绪,请总版主大人再帮帮忙,谢谢:)


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-18 21:55 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那个例子的代码修正如下:

public class GroupableHeaderExample extends JFrame {

GroupableHeaderExample() {
super( "Groupable Header Example" );

DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][]{
{"119","foo","bar","ja","ko","zh"},
{"911","bar","foo","en","fr","pt"}},
new Object[]{"SNo.","1","2","Native","2","3"});

JTable table = new JTable( dm ) {
protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};
TableColumnModel cm = table.getColumnModel();
ColumnGroup g_name = new ColumnGroup("Name");
g_name.add(cm.getColumn(1));
g_name.add(cm.getColumn(2));
ColumnGroup g_lang = new ColumnGroup("Language");
g_lang.add(cm.getColumn(3));
ColumnGroup g_other = new ColumnGroup("Others");
g_other.add(cm.getColumn(4));
g_other.add(cm.getColumn(5));
g_lang.add(g_other);
GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
header.addColumnGroup(g_name);
header.addColumnGroup(g_lang);

TableCellRenderer renderer = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
if (header != null) {
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
setHorizontalAlignment(JLabel.CENTER);
setText((value == null) ? "" : value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
};

TableColumnModel model = table.getColumnModel();
for (int i=0;i<model.getColumnCount();i++) {
model.getColumn(i).setHeaderRenderer(renderer);
}
JScrollPane scroll = new JScrollPane( table );
getContentPane().add( scroll );
setSize( 400, 120 );
}

public static void main(String[] args) {
GroupableHeaderExample frame = new GroupableHeaderExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.setVisible(true);
}
}

应该没有问题了。


floater edited on 2004-10-18 22:18

作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-19 09:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真是太感谢了,其实我也知道是renderer的问题,但是就是不知道该怎样去向model中添加renderer,我想知道renderer, table, tablemodel, tableColumnModel,等等(可能没列全)这些类再构造表的时候会起什么作用,他们之间的关系,以及他们执行的顺序,一句话:就是想理解他们在构造table是所扮演的角色,多谢帮忙,


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-19 13:20 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我发现tame写得很不错,有机会就好好的研究一下,不过据说那个作者向一缕轻烟地消失在空中了,真是遗憾!

kavinwang edited on 2004-10-19 13:52

作者 Re:如何用JTable设置表头多行 [Re:kylin]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-10-19 22:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
you've been jumping up and down here recently, really have time to kill, ...

Just kidding, keep up the good work...

Here is a credit, you should be able to get into the restricted area ...

cheers,
floater - just a floater in UFO - unknown enemy, not a flying cow ...



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-19 22:40 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
SmileBig SmileCoolShyBig SmileSmile
thanks for your credit.



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-20 10:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
kavinwang :您好
我发现表头和所对应的列不能够对齐,这是怎么一回事



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-20 12:56 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
原因为在先前的header中已经考虑过margin的问题了,而后面重复累加了此margin。你可以更改GroupableTableHeaderUI的paint方法,删除对margin的累加。
本来我已经看到这种不能对齐的情况,没想到你真的来问。不过我还是鼓励你自己解决,实在不行的话,在来此问。
具体代码如下:

public void paint(Graphics g, JComponent c) {
Rectangle clipBounds = g.getClipBounds();
if (header.getColumnModel() == null) return;
// ((GroupableTableHeader)header).setColumnMargin();
int column = 0;
Dimension size = header.getSize();
Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
Hashtable h = new Hashtable();
// int columnMargin = header.getColumnModel().getColumnMargin();

Enumeration enumeration = header.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
cellRect.height = size.height;
cellRect.y = 0;
TableColumn aColumn = (TableColumn)enumeration.nextElement();
Enumeration cGroups = ((GroupableTableHeader)header).getColumnGroups(aColumn);
if (cGroups != null) {
int groupHeight = 0;
while (cGroups.hasMoreElements()) {
ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
Rectangle groupRect = (Rectangle)h.get(cGroup);
if (groupRect == null) {
groupRect = new Rectangle(cellRect);
Dimension d = cGroup.getSize(header.getTable());
groupRect.width = d.width;
groupRect.height = d.height;
h.put(cGroup, groupRect);
}
paintCell(g, groupRect, cGroup);
groupHeight += groupRect.height;
cellRect.height = size.height - groupHeight;
cellRect.y = groupHeight;
}
}
cellRect.width = aColumn.getWidth() ;//+ columnMargin;
if (cellRect.intersects(clipBounds)) {
paintCell(g, cellRect, column);
}
cellRect.x += cellRect.width;
column++;
}
}




作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-21 14:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我想改变某一个特定的单元格的背景色或前景色,对于每一列,可以自定义render来指定颜色,但是某一个特定的单元格我却不知道该如何去做,而且我要求动态的根据我的需要随时可以改变它的颜色,想大虾求助


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-21 14:26 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有个叫做tableCellRender的你可重新实现。
在JTable或者其它的Swing中,灵活到任一个部分都可定制,这也是Swing比swt好的原因(可能不正确),不过你要熟悉一个叫做MVC的设计模式,基本上swing都是建构在这个设计模式上的。



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-21 17:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
怎样当修改表格重某个单元格后,焦点离开,怎样判断它的值是否改变(是不是很弱智)


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-21 17:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵,我现在可以实现改变某一个特定的单元格的背景色或前景色了,兴奋ing


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-21 18:38 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
恭喜你,有进步!同时看到你进步我也很开心!


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-22 08:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢:), 还有当修改表格中某个单元格后,焦点离开,怎样判断它的值是否改变,因为当值改变需要作其他的处理


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-22 09:28 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
当修改表格的某个单元内容后,如果不确定,比如回车,实际上,table是不知到的。
你的这个要求我也没有做过,不过可以给点建议:
你可以对cellrender添加事件,当修改时,通知你的事件监听器。



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-22 11:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因为表中某些列是有其他列的值计算出来的,所以我想实现当修改某个单元格后就把相应的变化反映到与值关联的单元格上的,所以我想判断某个单元的值是否被改变,不过我现在不那样做了,我重写了JTable的setValueAt方法,仍然调用原先的方法,在后面添加了我想要做的处理,这样就解决了,


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-22 13:00 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
实际上,table的很多数据上的问题都可以通过talbemodel解决的,tablemodel做的很好,可以自己随便实现。


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-22 16:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
焦点再table中,如果按下"tab"健,自动向下一个单元格移动,我想知道这个监听事件是怎样添加的,在哪里做的这些操作,因为我想添加自己的动作


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-22 17:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
头痛啊,没想到有这么复杂的表,下面这张图单元格可以对应多行,不知道怎样实现,痛苦中


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-22 18:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
头痛啊,没想到有这么复杂的表,下面这张图单元格可以对应多行,不知道怎样实现,痛苦中,请大虾们给点意见,小生这厢有理了



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-23 22:08 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在tame中好像有几个不同的table分别对应这个表的不同形式,你可把它们结合起来进行实现。
我也只是建立,你这个表格实在是有点复杂。



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-25 15:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
tame中MultipleRowHeaderExample这个类好像是实现这个效果的,但是我运行起来却不是这样的,不知道是怎么回事


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-25 16:41 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那是什么样子?


作者 Re:如何用JTable设置表头多行 [Re:kylin]
kylin





发贴: 61
积分: 0
于 2004-10-25 17:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我看代码里是这样的
* ----------------------------------------------
* | SNo. |
* ----------------------------------------------
* | | 1 |
* | Name |-----------------------------------
* | | 2 |
* ----------------------------------------------
* | | 1 |
* | |-----------------------------------
* | Language | 2 |
* | |-----------------------------------
* | | 3 |
* ----------------------------------------------
但是实际运行起来是这样的,好像fixedTable没有作用



作者 Re:如何用JTable设置表头多行 [Re:kylin]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-10-26 09:43 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在AttributiveCellTableMode中暂时把setDataVector方法的内容用下面的代码替代:

public void setDataVector(Vector newData, Vector columnNames) {
if (newData == null)
throw new IllegalArgumentException("setDataVector() - Null parameter");

dataVector = (newData != null) ? newData : new Vector(0);
columnIdentifiers = (columnNames != null) ? columnNames : new Vector(0);

cellAtt = new DefaultCellAttribute(dataVector.size(),
columnIdentifiers.size());

newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
     TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}


关于对不齐的问题,解决的方式可以参考上一次的方法,希望你能自己搞定。



go to first page go to previous page  1   2   3  go to next page go to last page

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2016 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号
客服电话 0592-8750026    客服信箱 714923@qq.com    客服QQ 714923