当前位置: 技术问答>java相关
高分求助:关于自定义Table控件的问题!在线等待。。。
来源: 互联网 发布时间:2015-09-24
本文导语: 要做一个Table控件,里面需要实现如下功能: 1列排序(默认) 2列标题显示排序方向箭头(默认) 3列标题显示tooltip(默认) 4列标题隐藏 5列标题是否显示图片 6行头(默认) 7锁定前n列(默认) 8单元格合并 9表格...
要做一个Table控件,里面需要实现如下功能:
1列排序(默认)
2列标题显示排序方向箭头(默认)
3列标题显示tooltip(默认)
4列标题隐藏
5列标题是否显示图片
6行头(默认)
7锁定前n列(默认)
8单元格合并
9表格内显示图片及其他控件
10隔行不同色(默认)
该怎样在Table或TableModel中书写代码,使得开发过程中应用这些功能时书写的代码最少?
问题解决后,会再开个帖子送分!!
急!!!帮忙!!!
(刚刚发了个帖子,发到非技术问题了,呵呵)
===================================================================
AllForOne.freejacky
1列排序(默认)
2列标题显示排序方向箭头(默认)
3列标题显示tooltip(默认)
4列标题隐藏
5列标题是否显示图片
6行头(默认)
7锁定前n列(默认)
8单元格合并
9表格内显示图片及其他控件
10隔行不同色(默认)
该怎样在Table或TableModel中书写代码,使得开发过程中应用这些功能时书写的代码最少?
问题解决后,会再开个帖子送分!!
急!!!帮忙!!!
(刚刚发了个帖子,发到非技术问题了,呵呵)
===================================================================
AllForOne.freejacky
|
一个columnmodel class ,一个tableHeader class
看看以下代码
/* our custom column model */
private class YourTableColumnModel
extends DefaultTableColumnModel
{
private Hashtable hiddenColumns = null;
public YourTableColumnModel() {
super();
this.hiddenColumns = new Hashtable();
}
public void setColumnHidden(int tcNdx, boolean hide) {
TableColumn tc = YourTable.this.getColumn(tcNdx, true);
if (tc != null) {
if (!hide) {
this.hiddenColumns.remove(tc);
} else
if (!this.hiddenColumns.containsKey(tc)) {
this.hiddenColumns.put(tc, new HiddenTableColumn(tc));
}
super.totalColumnWidth = -1; // 'super.invalidateWidthCache()'
this.fireColumnMarginChanged();
}
}
public boolean isColumnHidden(int n) {
TableColumn tc = this.getColumn(n, false);
return (tc != null)?
(tc instanceof HiddenTableColumn) :
true; // a non-existent column is 'hidden'
}
public TableColumn getColumn(int ndx) {
return this.getColumn(ndx, false);
}
public TableColumn getColumn(int ndx, boolean realColumn) {
TableColumn tc = super.getColumn(ndx);
if (realColumn) {
return tc;
} else {
Object htc = this.hiddenColumns.get(tc);
return (htc != null)? (TableColumn)htc : tc;
}
}
public Enumeration getColumns() {
Enumeration enum = super.getColumns();
if (!this.hiddenColumns.isEmpty()) {
Vector v = new Vector();
for (;enum.hasMoreElements();) {
Object tc = enum.nextElement();
Object htc = this.hiddenColumns.get(tc);
v.add((htc != null)? htc : tc);
}
enum = v.elements();
}
return enum;
}
}
/* proxy column for actual columns which have been hidden */
private class HiddenTableColumn
extends TableColumn
{
private TableColumn realColumn = null;
public HiddenTableColumn(TableColumn tc) {
super(tc.getModelIndex(), 0);
this.setMinWidth(0);
this.setMaxWidth(0);
this.setResizable(false);
this.realColumn = tc;
}
public TableColumn getRealColumn() {
return this.realColumn;
}
}
/* -------------------------------------------------------------------------
** custom JTableHeader to enable Column title tooltips
*/
protected JTableHeader createDefaultTableHeader()
{
return new YourTableHeader(super.columnModel);
}
private class YourTableHeader
extends JTableHeader
{
public YourTableHeader() {
this(null);
this.setReorderingAllowed(false);
}
public YourTableHeader(TableColumnModel cm) {
super(cm);
this.setReorderingAllowed(false);
}
public String getToolTipText(MouseEvent me) {
String tip = null;
// XXX - ideally we would only display the column title as a
// tooltip if part of the title has been scrunched out of view.
int c = super.columnModel.getColumnIndexAtX(me.getPoint().x);
if ((c >= 0) && (c != getDescriptionColumn())) {
Object v = super.columnModel.getColumn(c).getHeaderValue();
if (v instanceof String) {
tip = (String)v;
}
}
return (tip != null)? tip : super.getToolTipText(me);
}
}
看看以下代码
/* our custom column model */
private class YourTableColumnModel
extends DefaultTableColumnModel
{
private Hashtable hiddenColumns = null;
public YourTableColumnModel() {
super();
this.hiddenColumns = new Hashtable();
}
public void setColumnHidden(int tcNdx, boolean hide) {
TableColumn tc = YourTable.this.getColumn(tcNdx, true);
if (tc != null) {
if (!hide) {
this.hiddenColumns.remove(tc);
} else
if (!this.hiddenColumns.containsKey(tc)) {
this.hiddenColumns.put(tc, new HiddenTableColumn(tc));
}
super.totalColumnWidth = -1; // 'super.invalidateWidthCache()'
this.fireColumnMarginChanged();
}
}
public boolean isColumnHidden(int n) {
TableColumn tc = this.getColumn(n, false);
return (tc != null)?
(tc instanceof HiddenTableColumn) :
true; // a non-existent column is 'hidden'
}
public TableColumn getColumn(int ndx) {
return this.getColumn(ndx, false);
}
public TableColumn getColumn(int ndx, boolean realColumn) {
TableColumn tc = super.getColumn(ndx);
if (realColumn) {
return tc;
} else {
Object htc = this.hiddenColumns.get(tc);
return (htc != null)? (TableColumn)htc : tc;
}
}
public Enumeration getColumns() {
Enumeration enum = super.getColumns();
if (!this.hiddenColumns.isEmpty()) {
Vector v = new Vector();
for (;enum.hasMoreElements();) {
Object tc = enum.nextElement();
Object htc = this.hiddenColumns.get(tc);
v.add((htc != null)? htc : tc);
}
enum = v.elements();
}
return enum;
}
}
/* proxy column for actual columns which have been hidden */
private class HiddenTableColumn
extends TableColumn
{
private TableColumn realColumn = null;
public HiddenTableColumn(TableColumn tc) {
super(tc.getModelIndex(), 0);
this.setMinWidth(0);
this.setMaxWidth(0);
this.setResizable(false);
this.realColumn = tc;
}
public TableColumn getRealColumn() {
return this.realColumn;
}
}
/* -------------------------------------------------------------------------
** custom JTableHeader to enable Column title tooltips
*/
protected JTableHeader createDefaultTableHeader()
{
return new YourTableHeader(super.columnModel);
}
private class YourTableHeader
extends JTableHeader
{
public YourTableHeader() {
this(null);
this.setReorderingAllowed(false);
}
public YourTableHeader(TableColumnModel cm) {
super(cm);
this.setReorderingAllowed(false);
}
public String getToolTipText(MouseEvent me) {
String tip = null;
// XXX - ideally we would only display the column title as a
// tooltip if part of the title has been scrunched out of view.
int c = super.columnModel.getColumnIndexAtX(me.getPoint().x);
if ((c >= 0) && (c != getDescriptionColumn())) {
Object v = super.columnModel.getColumn(c).getHeaderValue();
if (v instanceof String) {
tip = (String)v;
}
}
return (tip != null)? tip : super.getToolTipText(me);
}
}
|
这里肯定能找到你要的东西:
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
|
我觉得你实现一个columnmodel比较好,来控制列。
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html上面有详细的code
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html上面有详细的code