让Swing表格支持远程后台数据翻页-2

上一篇 / 下一篇  2012-08-13 10:32:06 / 个人分类:杂谈

!\2Rz#bQ2cOc0  在上面代码中,所有的翻页函数,都会集中调用loadPage()这个函数,从后台真正获取数据。而函数getTotalRowCount则负责51Testing软件测试网+M#U_*w#|*D6Qo

51Testing软件测试网p[$apJ H&HeIU

  从后台获得“一共有多少条记录”。其他函数,基本进行转发即可。

|/~'e#H!{*t4TQ(I7q051Testing软件测试网 u2~z3S2sew&s

  另外一个需要注意的就是removePageListener/addPageListener等函数。主要用于对监听器进行管理,包括注册、删除、触发通知等等。

ve q? f'r\$H0

]4h&Jw o2c2n0  这些也是必须要实现的,不过很简单,用一个ArrayList维护就行了,触发时间时候,直接遍历、回调即可。

zXi?kD9^R051Testing软件测试网Tc1bo)t8B0b

  具体通过SQL从后台调用数据的实现,这里就不介绍了。相信每一个实际项目都有不同的数据库、接口、调用方法方面的差别。这里只是点到为止。51Testing软件测试网uJ6] M |%sF

R%N4N`e/IIL1O0  有了这个翻页器,我们就可以直接用在表格中了。下面继承一个表格,并用这个翻页器。

'w ^c?:XD%]0

Ao%z:xa+f/D{ F#ac051Testing软件测试网$hurX%M

1 public class AddressTable extends TElementTable {
#B~yz,t[?Ime'o02     
NvPd&~*S4q!RK/UF03     public AddressTablePaging getTablePaging() {
S(O2`$j f8hF+a04         return tablePaging;
1Z|:e uX-GQT05     }51Testing软件测试网vcN6Fu9Di v
6     51Testing软件测试网!uLu;Ar+P:]
7 }
1ZB/D:w!l5BU08
51Testing软件测试网^T_U&rZ3C

  这样,默认翻页器被替换,新的后台翻页器被置入表格中。最后,再通过本文最开始提供的两行代码把表格放入界面中,程序就基本完成了。

$}b[&[a+}$l051Testing软件测试网 ]&g7t~,Ew

,A$Liz s\6Wk0
1 int[] pageSizes = { 100, 500, 1000 };51Testing软件测试网{ i/y,h6`"~q
2 this.add(new TPageNavigator(table.getTablePaging(), pageSizes), BorderLayout.CENTER);

%m"T RwG%T\!q0  其中pageSizes数组是定义了界面上每页条数的下拉列表选项,我们可以根据实际应用自己设置,如下图:

'[J-X.aTTa9Dl0

AxAt4C"UwF-P1F0

  这样,一个完整的后台翻页程序就完成了。给表格设置好列、在后台添加一些数据,跑起来会是这样:

8RN*k%gFA4tD[^0

P]g]n{!S0

  如果再增加一些查询字段等,就更帅了:

;er R\$~0

/fn J w-gQ4P*r#X0

  对了,最后,再顺便给大家介绍一下表格中的可点击连接是如何做的:51Testing软件测试网!PIa*o0a ~pU

51Testing软件测试网j P%SM-y^4Oc:U

~/AWZ+]5n%K0  要做这种可点击链接,首先要做3件事:1是显示link,2是显示手形状的光标,3是响应鼠标点击动作。对于1,可以来个“釜底抽薪”:在表格上从根儿上拦截prepareRenderer,然后对文字动态修改为html的a标签的连接方式进行处理:51Testing软件测试网[E ? @I4vc7H&ig

51Testing软件测试网,Q*^B8qSxJW n];K^

6D-@J7c'kp`;lW0
 1 @Override51Testing软件测试网d;p"m#|$BO]w
 2 public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {51Testing软件测试网z x5o6q7i
 3     Element element = this.getElementByRowIndex(row);
$gq)wZa)?5}:W0 4     AddressVO vo = (AddressVO) element.getBusinessObject();
6Y!zGTq0 5     Component com = super.prepareRenderer(renderer, row, column);
3Myx2W8G{0 6     if (!vo.isValid()) {
&k] GAP0 7         com.setForeground(Color.red);
C @I!g3n O0 8     } else {51Testing软件测试网-~ KJ m6pO5Q8}1G5y
 9         com.setForeground(Color.black);51Testing软件测试网O-x@$}-F\6f(}K7Z F
10     }
fA2J"e*Iqo8o"E q011 51Testing软件测试网 O+Vc3F4vj
12     if (column == 2 || column == 3) {
Ijr)|!`*\013         String text = ((JLabel) com).getText();
?@&?t&~ jb xF014         text = "<html><a href=\"#\">" + text + "</a></html>";
e5q_c#TVQw6?G015         ((JLabel) com).setText(text);51Testing软件测试网-IkJCoK X
16     }
E YC6I6KU6Y:xv017     return com;51Testing软件测试网_({4`B rrw l
18 }51Testing软件测试网~~a Z]K@ \t+j
19

!o5TL2e mIoG#gG0  对于手形光标,可以通过监听鼠标移动,是否位于链接文字上方来动态修改光标:51Testing软件测试网"X,{ ]4KpK

g-mCI'\3KeA051Testing软件测试网"w \!W+BQ(DG

 1 this.addMouseMotionListener(new MouseMotionAdapter() {
1iJ J$HO%Q0 2     @Override
.J~ J`Q"~ ^D x:q0 3     public void mouseMoved(MouseEvent e) {
K6C/H"fDYK0 4         int row = rowAtPoint(e.getPoint());
*B#}E6a4Bt \|`1j c0 5         int column = columnAtPoint(e.getPoint());
3g!| JYl4y"t(TC)y0 6         setCursor(Cursor.getDefaultCursor());
X n$Ta(C g0 7         if (row >= 0 && column >= 0) {51Testing软件测试网8zo$l$BE&M8D
 8             if (column == 2 || column == 3) {51Testing软件测试网 D8c6K5_$bri's#e
 9                 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
&tjdq1a'iqMGZ010             }
F U+H t:N9_011         }
0XO([Y;{Z O(z2M012     }
JLFT^013 });
51Testing软件测试网ub_8B&s&k

  最后,对于点击动作,可以通过给table添加鼠标监听器完成:

&vjGA Xh`!Gs0

!zm2a)w"NuV0

:n)C:c/Nrczah0k0
 1 this.addMouseListener(new MouseAdapter() {
(l(y4P"? ^L4@0 2     @Override51Testing软件测试网0y1`m:yde0Sv
 3     public void mouseClicked(MouseEvent e) {
)sb#S2Fwi'@P+O0 4         int row = rowAtPoint(e.getPoint());51Testing软件测试网u1{NV[d9X
 5         int column = columnAtPoint(e.getPoint());51Testing软件测试网O:u;G#SY"rg~ y5Bl
 6         setCursor(Cursor.getDefaultCursor());
_Q%r!qaOG0 7         if (row >= 0 && column >= 0) {51Testing软件测试网*sh(ko\)xY)w'j
 8             if (column == 2 || column == 3) {
"\\'_-z(V3[f,V3?#p0 9                 Object value = getValueAt(row, column);51Testing软件测试网W*I2eH/W
10                 //do your action here.
i2`)T0pG {0
11             }51Testing软件测试网$T Wsx+T1oO P g#Y
12         }51Testing软件测试网6F6L*M^3d,l
13     }51Testing软件测试网amQ7[a9C_3H&I,Ff
14 });51Testing软件测试网"E`6` [ D&wI
15
51Testing软件测试网i|2Or*e#Hy_#HA

  至此,一个完整的可后台翻页、可鼠标点击超链接的综合型table就完成了。在实际使用中,还可以增加更复杂的翻页和显示效果。例如在 TWaver的兄弟产品2BizBox免费ERP软件中,就有大量这样的应用,感兴趣的朋友可以到2BizBox.cn去下载安装一个玩一玩。

M Jd8N{0

A\C~eN?P[2u0

TAG:

 

评分:0

我来说两句

Open Toolbar