|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
再举这样一个例子:如果你想对一个数字取绝对值,你会怎么做呢?java的做法是intc=Math.abs(-166);而ruby的做法是:c=-166.abs。呵呵,这就看出了java与ruby的区别。显现起首声明,这些内容都是自创来的,略微修改了一下。
次要就是三个使用类ColumnGroup,GroupableTableHeader,GroupableTableHeaderU
和一个示例类GroupableHeaderExample
1、ColumnGroup类次要卖力多表头的构建
importjava.awt.Component;
importjava.awt.Dimension;
importjava.util.Enumeration;
importjava.util.Vector;
importjavax.swing.JLabel;
importjavax.swing.JTable;
importjavax.swing.UIManager;
importjavax.swing.table.DefaultTableCellRenderer;
importjavax.swing.table.JTableHeader;
importjavax.swing.table.TableCellRenderer;
importjavax.swing.table.TableColumn;
publicclassColumnGroup{
protectedTableCellRendererrenderer;
protectedVectorv;
protectedStringtext;
protectedintmargin=0;
publicColumnGroup(Stringtext){
this(null,text);
}
publicColumnGroup(TableCellRendererrenderer,Stringtext){
if(renderer==null){
this.renderer=newDefaultTableCellRenderer(){
publicComponentgetTableCellRendererComponent(JTabletable,
Objectvalue,booleanisSelected,booleanhasFocus,
introw,intcolumn){
JTableHeaderheader=table.getTableHeader();
if(header!=null){
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
setHorizontalAlignment(JLabel.CENTER);
this.setText((value==null)?"":value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
returnthis;
}
};
}else{
this.renderer=renderer;
}
this.text=text;
v=newVector();
}
/**
*@paramobj
*TableColumnorColumnGroup
*/
publicvoidadd(Objectobj){
if(obj==null){
return;
}
v.addElement(obj);
}
/**
*@paramc
*TableColumn
*@paramv
*ColumnGroups
*/
publicVectorgetColumnGroups(TableColumnc,Vectorg){
g.addElement(this);
if(v.contains(c))
returng;
Enumerationenumeration=v.elements();
while(enumeration.hasMoreElements()){
Objectobj=enumeration.nextElement();
if(objinstanceofColumnGroup){
Vectorgroups=(Vector)((ColumnGroup)obj).getColumnGroups(c,
(Vector)g.clone());
if(groups!=null)
returngroups;
}
}
returnnull;
}
publicTableCellRenderergetHeaderRenderer(){
returnrenderer;
}
publicObjectgetHeaderValue(){
returntext;
}
publicintgetSize(){
returnv==null?0:v.size();
}
publicDimensiongetSize(JTabletable){
Componentcomp=renderer.getTableCellRendererComponent(table,
getHeaderValue(),false,false,-1,-1);
intheight=comp.getPreferredSize().height;
intwidth=0;
Enumerationenumeration=v.elements();
while(enumeration.hasMoreElements()){
Objectobj=enumeration.nextElement();
if(objinstanceofTableColumn){
TableColumnaColumn=(TableColumn)obj;
width+=aColumn.getWidth();
width+=margin;
}else{
width+=((ColumnGroup)obj).getSize(table).width;
}
}
returnnewDimension(width,height);
}
/**
*
*创立日期:(2003-7-1410:53:26)
*
*@returnjava.lang.String
*/
publicjava.lang.StringgetText(){
returntext;
}
/**
*列表头中删除Column创立日期:(2002-9-2515:49:00)
*/
publicbooleanremoveColumn(ColumnGroupptg,TableColumntc){
booleanretFlag=false;
if(tc!=null){
for(inti=0;i<ptg.v.size();i++){
ObjecttmpObj=ptg.v.get(i);
if(tmpObjinstanceofColumnGroup){
retFlag=removeColumn((ColumnGroup)tmpObj,tc);
//假如找到前往
if(retFlag)
break;
}elseif(tmpObjinstanceofTableColumn){
//判别是不是查找的对象
if(tmpObj==tc){
ptg.v.remove(i);
retFlag=true;
break;
}
}
}
}
returnretFlag;
}
/**
*列表头中删除ColumnGrp删除前往true,不然前往false;创立日期:(2002-9-2515:49:00)
*/
publicbooleanremoveColumnGrp(ColumnGroupptg,ColumnGrouptg){
booleanretFlag=false;
if(tg!=null){
for(inti=0;i<ptg.v.size();i++){
ObjecttmpObj=ptg.v.get(i);
if(tmpObjinstanceofColumnGroup){
//判别是不是查找的对象
if(tmpObj==tg){
ptg.v.remove(i);
retFlag=true;
break;
}else{
retFlag=removeColumnGrp((ColumnGroup)tmpObj,tg);
//假如找到前往
if(retFlag)
break;
}
}elseif(tmpObjinstanceofTableColumn){
break;
}
}
}
returnretFlag;
}
publicvoidsetColumnMargin(intmargin){
this.margin=margin;
Enumerationenumeration=v.elements();
while(enumeration.hasMoreElements()){
Objectobj=enumeration.nextElement();
if(objinstanceofColumnGroup){
((ColumnGroup)obj).setColumnMargin(margin);
}
}
}
publicvoidsetHeaderRenderer(TableCellRendererrenderer){
if(renderer!=null){
this.renderer=renderer;
}
}
/**
*
*创立日期:(2003-7-1410:53:26)
*
*@paramnewText
*java.lang.String
*/
publicvoidsetText(java.lang.StringnewText){
text=newText;
}
}
2、GroupableHeader承继自JTableHeader,为多表头布局的办理者
importjava.util.Enumeration;
importjava.util.Vector;
importjavax.swing.table.JTableHeader;
importjavax.swing.table.TableColumn;
importjavax.swing.table.TableColumnModel;
/**
*GroupableTableHeader
*@authorflyx
*/
publicclassGroupableTableHeaderextendsJTableHeader{
privatestaticfinalStringuiClassID="GroupableTableHeaderUI";
protectedVectorcolumnGroups=null;
publicGroupableTableHeader(TableColumnModelmodel){
super(model);
setUI(newGroupableTableHeaderUI());
setReorderingAllowed(false);
setRequestFocusEnabled(false);
}
publicvoidaddColumnGroup(ColumnGroupg){
if(columnGroups==null){
columnGroups=newVector();
}
columnGroups.addElement(g);
}
/**
*
*创立日期:(2001-8-3115:54:29)
*/
publicvoidclearColumnGroups(){
columnGroups=null;
}
publicColumnGroup[]getColumnGroups(){
ColumnGroup[]retg=null;
if(columnGroups.size()>0){
retg=newColumnGroup[columnGroups.size()];
columnGroups.copyInto(retg);
}
returnretg;
}
publicEnumerationgetColumnGroups(TableColumncol){
if(columnGroups==null)
returnnull;
Enumerationenum=columnGroups.elements();
while(enum.hasMoreElements()){
ColumnGroupcGroup=(ColumnGroup)enum.nextElement();
Vectorv_ret=(Vector)cGroup.getColumnGroups(col,newVector());
if(v_ret!=null){
returnv_ret.elements();
}
}
returnnull;
}
/**
*Identifieswhetherornotthiscomponentcanreceivethefocus.
*Adisabledbutton,forexample,wouldreturnfalse.
*
*@returntrueifthiscomponentcanreceivethefocus
*/
publicbooleanisFocusTraversable(){
returnsuper.isFocusTraversable()&&isRequestFocusEnabled();
}
publicvoidsetColumnMargin(){
if(columnGroups==null)
return;
intcolumnMargin=getColumnModel().getColumnMargin();
Enumerationenum=columnGroups.elements();
while(enum.hasMoreElements()){
ColumnGroupcGroup=(ColumnGroup)enum.nextElement();
cGroup.setColumnMargin(columnMargin);
}
}
publicvoidsetReorderingAllowed(booleanb){
reorderingAllowed=b;
}
}
3、GroupableTableHeaderUI承继自BasicTableHeaderUI,是多表头的画匠
importjava.awt.Component;
importjava.awt.Dimension;
importjava.awt.Graphics;
importjava.awt.Rectangle;
importjava.util.Enumeration;
importjava.util.Hashtable;
importjava.util.Vector;
importjavax.swing.JComponent;
importjavax.swing.JLabel;
importjavax.swing.JTable;
importjavax.swing.UIManager;
importjavax.swing.plaf.basic.BasicTableHeaderUI;
importjavax.swing.table.*;
/**
*多表头TableHeaderUI
*
*@authorflyx
*/
publicclassGroupableTableHeaderUIextendsBasicTableHeaderUI{
privateintm_height;
privateDimensioncreateHeaderSize(longwidth){
TableColumnModelcolumnModel=header.getColumnModel();
width+=columnModel.getColumnMargin()*columnModel.getColumnCount();
if(width>Integer.MAX_VALUE){
width=Integer.MAX_VALUE;
}
returnnewDimension((int)width,getHeaderHeight());
}
/**
*取得该多表头下的基础列数(zjb)递回创立日期:(02-4-818:03:56)
*
*@returnint
*@paramcg
*nc.ui.pub.beans.table.ColumnGroup
*/
privateintgetColCountUnderColGroup(ColumnGroupcg,intiCount){
Vectorv=cg.v;
for(inti=0;i<v.size();i++){
Objectobj=v.elementAt(i);
if(objinstanceofColumnGroup)
iCount=getColCountUnderColGroup((ColumnGroup)obj,iCount);
else
iCount++;
}
returniCount;
}
publicintgetHeaderHeight(){
intheight=0;
TableColumnModelcolumnModel=header.getColumnModel();
for(intcolumn=0;column<columnModel.getColumnCount();column++){
TableColumnaColumn=columnModel.getColumn(column);
TableCellRendererrenderer=aColumn.getHeaderRenderer();
//
if(renderer==null){
renderer=newDefaultTableCellRenderer(){
publicComponentgetTableCellRendererComponent(
JTabletable,Objectvalue,booleanisSelected,
booleanhasFocus,introw,intcolumn){
JTableHeaderheader=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"));
returnthis;
}
};
}
//
Componentcomp=renderer.getTableCellRendererComponent(header
.getTable(),aColumn.getHeaderValue(),false,false,-1,
column);
intcHeight=comp.getPreferredSize().height;
Enumerationenumeration=((GroupableTableHeader)header)
.getColumnGroups(aColumn);
if(enumeration!=null){
while(enumeration.hasMoreElements()){
ColumnGroupcGroup=(ColumnGroup)enumeration
.nextElement();
cHeight+=cGroup.getSize(header.getTable()).height;
}
}
height=Math.max(height,cHeight);
}
height=Math.max(height,m_height);
returnheight;
}
publicDimensiongetPreferredSize(JComponentc){
longwidth=0;
Enumerationenumeration=header.getColumnModel().getColumns();
while(enumeration.hasMoreElements()){
TableColumnaColumn=(TableColumn)enumeration.nextElement();
width=width+aColumn.getWidth();
}
returncreateHeaderSize(width);
}
publicvoidpaint(Graphicsg,JComponentc){
RectangleclipBounds=g.getClipBounds();
if(header.getColumnModel()==null)
return;
((GroupableTableHeader)header).setColumnMargin();
intcolumn=0;
Dimensionsize=header.getSize();
RectanglecellRect=newRectangle(0,0,size.width,size.height);
Hashtableh=newHashtable();
intcolumnMargin=header.getColumnModel().getColumnMargin();
Enumerationenumeration=header.getColumnModel().getColumns();
while(enumeration.hasMoreElements()){
cellRect.height=size.height;
cellRect.y=0;
TableColumnaColumn=(TableColumn)enumeration.nextElement();
EnumerationcGroups=((GroupableTableHeader)header)
.getColumnGroups(aColumn);
if(cGroups!=null){
intgroupHeight=0;
while(cGroups.hasMoreElements()){
ColumnGroupcGroup=(ColumnGroup)cGroups.nextElement();
RectanglegroupRect=(Rectangle)h.get(cGroup);
if(groupRect==null){
groupRect=newRectangle(cellRect);
Dimensiond=cGroup.getSize(header.getTable());
if(!System.getProperty("java.vm.version").startsWith(
"1.2")){
intiColCount=getColCountUnderColGroup(cGroup,0);//取得该多表头下的基础列数(zjb)
//System.out.println(iColCount);
groupRect.width=d.width-iColCount
*columnMargin;
}else
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;
}
}
if(!System.getProperty("java.vm.version").startsWith("1.2"))
cellRect.width=aColumn.getWidth();
else
cellRect.width=aColumn.getWidth()+columnMargin;
if(cellRect.intersects(clipBounds)){
paintCell(g,cellRect,column);
}
cellRect.x+=cellRect.width;
column++;
}
}
privatevoidpaintCell(Graphicsg,RectanglecellRect,intcolumnIndex){
TableColumnaColumn=header.getColumnModel().getColumn(columnIndex);
TableCellRendererrenderer=aColumn.getHeaderRenderer();
//
if(renderer==null){
renderer=newDefaultTableCellRenderer(){
publicComponentgetTableCellRendererComponent(JTabletable,
Objectvalue,booleanisSelected,booleanhasFocus,
introw,intcolumn){
JTableHeaderheader=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"));
returnthis;
}
};
}
//
//Componentcomponent=
//renderer.getTableCellRendererComponent(header.getTable(),
//aColumn.getHeaderValue(),false,false,-1,columnIndex);
//
StringheaderValue=aColumn.getHeaderValue().toString();
Componentcomponent=renderer.getTableCellRendererComponent(header
.getTable(),headerValue,false,false,-1,columnIndex);
rendererPane.add(component);
rendererPane.paintComponent(g,component,header,cellRect.x,
cellRect.y,cellRect.width,cellRect.height,true);
}
privatevoidpaintCell(Graphicsg,RectanglecellRect,ColumnGroupcGroup){
TableCellRendererrenderer=cGroup.getHeaderRenderer();
//
if(renderer==null){
renderer=newDefaultTableCellRenderer(){
publicComponentgetTableCellRendererComponent(JTabletable,
Objectvalue,booleanisSelected,booleanhasFocus,
introw,intcolumn){
JTableHeaderheader=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"));
returnthis;
}
};
}
//
//Componentcomponent=
//renderer.getTableCellRendererComponent(header.getTable(),
//cGroup.getHeaderValue(),false,false,-1,-1);
//
StringheaderValue=cGroup.getHeaderValue().toString();
Componentcomponent=renderer.getTableCellRendererComponent(header
.getTable(),headerValue,false,false,-1,-1);
rendererPane.add(component);
rendererPane.paintComponent(g,component,header,cellRect.x,
cellRect.y,cellRect.width,cellRect.height,true);
}
publicvoidsetHeaderHeight(intiHeight){
m_height=iHeight;
}
}
4、一个复杂的测试用例GroupableHeaderExample
importjava.awt.Color;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;
importjavax.swing.table.DefaultTableModel;
importjavax.swing.table.JTableHeader;
importorg.f5.ui.table.ColumnGroup;
importorg.f5.ui.table.GroupableTableHeader;
/**
*@version1.011/09/98
*/
publicclassGroupableHeaderExampleextendsJFrame{
GroupableHeaderExample(){
super("GroupableHeaderExample");
initialize();
}
privatevoidinitialize(){
DefaultTableModeldm=newDefaultTableModel();
dm.setDataVector(newObject[][]{
{"119","foo","bar","ja","ko","zh"},
{"911","bar","foo","en","fr","pt"}},newObject[]{
"SNo.","1","2","Native","2","3"});
JTabletable=newJTable(dm){
protectedJTableHeadercreateDefaultTableHeader(){
returnnewGroupableTableHeader(columnModel);
}
};
GroupableTableHeaderheader=(GroupableTableHeader)table
.getTableHeader();
javax.swing.table.TableColumnModelcm=table.getColumnModel();
//承受范例
ColumnGroupg_jslx=newColumnGroup(
"吸收范例");
g_jslx.add(cm.getColumn(1));
g_jslx.add(cm.getColumn(2));
header.addColumnGroup(g_jslx);
JScrollPanescroll=newJScrollPane(table);
getContentPane().add(scroll);
setSize(400,120);
//设置列头背景为白色
header.setBackground(Color.RED);
}
publicstaticvoidmain(String[]args){
GroupableHeaderExampleframe=newGroupableHeaderExample();
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
frame.setVisible(true);
}
}
5、最终效果
等我会贴图了再贴下去吧
再说说缺点:首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|