Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Swing tables support remote background data paging

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to make the Swing table support remote background data paging? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

TWaver Java not only provides table components such as TTable and TElementTable, but also provides a table pager TPageNavigator. Let the table and page flip work together, you can immediately make a very standard pageable table interface, as shown in the following figure.

To make the two components work together, just new an instance like this and put it on the interface:

TElementTable table = new TElementTable (); int [] pageSizes = {100,500,1000}; TPageNavigator nav=new TPageNavigator (table.getTablePaging (), pageSizes)

Nav is a normal JPanel that can be placed anywhere in the interface. On the other hand, table and nav can be completely separated from the display on the interface, and you can lay out whatever you want.

However, this table that can turn pages can only turn pages of local data. In other words, it can only translate data that has been put into the TDataBox. For example, we add 10000 pieces of data to TDataBox at one time, and we can use this page flip machine to perform page flipping operations such as "100 pieces per page, a total of 100 pages". But most of the time, what we need is not "local page turning", but "remote page turning". The so-called remote page flipping, that is, each time the page is turned, the TDataBox data needs to be emptied, and the "next page" data is dynamically retrieved from the remote server for TDataBox loading and display.

How do you do this? As long as you use the "artifact" of TWaver Java, it is not difficult to do this. This paper uses an example to illustrate how to customize a page flip to intercept the page flip action, and obtain the page flip data from the server for dynamic display.

The page turning of TElementTable is actually accomplished by an interface of TablePaging. TWaver Java does local page flipping with a default TablePaging implementation. All we have to do is create a TablePaging that turns pages remotely instead of the default implementation.

The TablePaging interface defines the following functions. Most functions ask you simple remote data questions: how many records are there? How many records are there on a page? How many pages are there? What page is it now?

And call back this API when operations such as * * page, * * page, previous page, next page, and so on occur. Therefore, as long as we have background data, it is not difficult to answer these questions.

Public interface TablePaging {public int getCurrentPageIndex (); public void setCurrentPageIndex (int currentPageIndex); public int getPageRowSize (); public void setPageRowSize (int pageRowSize); public int getPageTotalCount (); public int getTotalRowCount (); public void firstPage (); public void previousPage (); public void nextPage (); public void lastPage (); public void update (); public void addPageListener (PageListener pageListener); public void removePageListener (PageListener pageListener);}

The above functions can basically be understood as the name implies, so there is no need to introduce them. Let's now assume that there is a database in the background with a table of the customer's address. Through a SQL query service, we can get these paging data. Based on this assumption, we can do the following implementation.

Public class AddressTablePaging implements TablePaging {private SearchPane parent = null; private List pageListeners = new ArrayList (); private TElementTable table = null; private int pageIndex = 1; private int pageSize = 100; public AddressTablePaging (TElementTable table, SearchPane parent) {this.table = table; this.parent = parent;} private void loadPage () {table.getDataBox () .clear () Try {int start = (pageIndex-1) * pageSize; Collection data = Server.searchAddress (..); for (AddressVO vo: data) {Node node = new Node (); node.setBusinessObject (vo); table.getDataBox () .addElement (node) } catch (Exception ex) {ex.printStackTrace (); JOptionPane.showMessageDialog (table, ex.getMessage ());} firePageChanged ();} @ Override public void firstPage () {pageIndex = 1; loadPage () } @ Override public int getCurrentPageIndex () {return this.pageIndex;} @ Override public int getPageRowSize () {return this.pageSize;} @ Override public int getPageTotalCount () {try {int totalCount = getTotalRowCount (); int pageCount = totalCount / getPageRowSize () If (totalCount% getPageRowSize () > 0) {pageCount++;} return pageCount;} catch (Exception ex) {ex.printStackTrace ();} return 0 } @ Override public int getTotalRowCount () {try {return Server.getAddressTotalCount (.);} catch (Exception ex) {ex.printStackTrace ();} return 0;} @ Override public void lastPage () {this.pageIndex = getPageTotalCount (); this.loadPage () } @ Override public void nextPage () {this.pageIndex++; this.loadPage ();} @ Override public void previousPage () {if (this.pageIndex > 1) {pageIndex--;} this.loadPage () } @ Override public void setCurrentPageIndex (int pageIndex) {this.pageIndex = pageIndex;} @ Override public void setPageRowSize (int pageSize) {this.pageSize = pageSize;} @ Override public void addPageListener (PageListener pageListener) {this.pageListeners.add (pageListener) } @ Override public void removePageListener (PageListener pageListener) {this.pageListeners.remove (pageListener);} public void firePageChanged () {for (int I = 0; I)

< this.pageListeners.size(); i++) { PageListener pageListener = (PageListener) this.pageListeners.get(i); pageListener.pageChanged(); } } @Override public void update() { } } 在上面代码中,所有的翻页函数,都会集中调用loadPage()这个函数,从后台真正获取数据。而函数getTotalRowCount则负责 从后台获得"一共有多少条记录"。其他函数,基本进行转发即可。 另外一个需要注意的就是removePageListener/addPageListener等函数。主要用于对监听器进行管理,包括注册、删除、触发通知等等。 这些也是必须要实现的,不过很简单,用一个ArrayList维护就行了,触发时间时候,直接遍历、回调即可。 具体通过SQL从后台调用数据的实现,这里就不介绍了。相信每一个实际项目都有不同的数据库、接口、调用方法方面的差别。这里只是点到为止。 有了这个翻页器,我们就可以直接用在表格中了。下面继承一个表格,并用这个翻页器。 public class AddressTable extends TElementTable { public AddressTablePaging getTablePaging() { return tablePaging; } } 这样,默认翻页器被替换,新的后台翻页器被置入表格中。***,再通过本文最开始提供的两行代码把表格放入界面中,程序就基本完成了。 int[] pageSizes = { 100, 500, 1000 }; this.add(new TPageNavigator(table.getTablePaging(), pageSizes), BorderLayout.CENTER); 其中pageSizes数组是定义了界面上每页条数的下拉列表选项,我们可以根据实际应用自己设置,如下图:

In this way, a complete background page flipping program is completed. Set up columns for the table, add some data in the background, and it will run like this (turn the page to watch)

If you add more query fields, etc., it will be even more handsome:

By the way, I'd like to tell you how the clickable links in the table are done:

To do this clickable link, you first need to do three things: 1 is to show that link,2 is the cursor that displays the shape of the hand, and 3 is to respond to mouse clicks. For 1, you can get to the bottom of it: intercept prepareRenderer from the root on the table and then deal with the connection of the a tag whose text is dynamically changed to html:

@ Override public Component prepareRenderer (TableCellRenderer renderer, int row, int column) {Element element = this.getElementByRowIndex (row); AddressVO vo = (AddressVO) element.getBusinessObject (); Component com = super.prepareRenderer (renderer, row, column); if (! vo.isValid ()) {com.setForeground (Color.red);} else {com.setForeground (Color.black) } if (column = = 2 | | column = = 3) {String text = ((JLabel) com) .getText (); text = "" + text + ""; ((JLabel) com) .setText (text);} return com;}

For the hand cursor, you can dynamically modify the cursor by listening for mouse movement and whether it is over the linked text:

This.addMouseMotionListener (new MouseMotionAdapter () {@ Override public void mouseMoved (MouseEvent e) {int row = rowAtPoint (e.getPoint ()); int column = columnAtPoint (e.getPoint ()); setCursor (Cursor.getDefaultCursor () If (row > = 0 & & column > = 0) {if (column = = 2 | | column = = 3) {setCursor (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR));}})

*. For click actions, you can add mouse listeners to table:

This.addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {int row = rowAtPoint (e.getPoint ()); int column = columnAtPoint (e.getPoint ()); setCursor (Cursor.getDefaultCursor ()); if (row > = 0 & & column > = 0) {if (column = = 2 | column = = 3) {Object value = getValueAt (row, column) / / do your action here. })

At this point, a complete background page can be turned, the mouse can click on the hyperlink of the comprehensive table is completed. In practical use, you can also add more complex page flipping and display effects. For example, in TWaver's brother product 2BizBox free ERP software, there are a large number of such applications, interested friends can go to 2BizBox.cn to download and install a play.

This is the answer to the question about how to make the Swing table support remote background data paging. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report