java swing jtable分页,具有多个JTable的大型JScrollPane-如何分页

I'vve been trying to google this, but most people ask questions related to a large single table, my problem is a bit different.

Sorry I cant provide working code, my program is a bit complex, but if someone can point me in a direction ill try to find a solution.

In short, I have a very large text file that i read and create objects for each line, eg HeaderRecord or ClaimDetail etc. the current file ends up with about 60,000 objects. I then create JTables for each record, iterate through them and then add them to my scrollpane.

It works great, my problem is that loading this data into memory takes alot! (obviously). But even ignoring that, after the data is loading and the screen pops up its still stuck for like a minute, probably trying to draw all of this is the pane.

The only solution i can think of is to somehow paginate the display. only pulling in a certain number of JTables at a time. I have no idea how to go about this! If someone can please help me out with a suggestion of what to look at i would really appreciate it

Thanks

Update: I implemented a swingworker like this:

private class TableRun extends SwingWorker {

private ArrayDeque fileRecords;

private final GridBagConstraints gc;

private final JPanel parentPanel;

int counter = 1;

TableRun(ArrayDeque fileRecords, GridBagConstraints gc, JPanel parentPanel) {

this.fileRecords = fileRecords;

this.gc = gc;

this.parentPanel = parentPanel;

}

@Override

protected Void doInBackground() {

Iterator iterator = fileRecords.iterator();

while (iterator.hasNext()) {

publish(getTabel(iterator.next()));

Thread.yield();

}

return null;

}

@Override

protected void process(final List tables) {

Iterator iterator = tables.iterator();

while(iterator.hasNext()) {

JTable table = iterator.next();

gc.fill = 1;

parentPanel.add(table.getTableHeader(), gc);

gc.gridy++;

parentPanel.add(table,gc);

gc.gridy++;

System.out.println("Sequence Nr : " + table.getModel().getValueAt(0,1) + " - Counter :" + counter++);

}

}

}

It seems to work... mostly. my problem is that in my JFrame constructor i set

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

so the frame opens up on the entire screen. What i want is as the swingworker completes adding the JTable to my panel in the process method it should add those tables to the panel and display them. so it should display as it gets them all the way up until the entire file is read. This doesnt happen. In fact the display just stays blank until i resize the frame, then it actually displays the tables.

I tried to call Outerclass.this.repaint() from within the process method in order to rapaint my Jframe but that didnt work...

Any advice?

Thanks

解决方案

A complete design in beyond the scope of SO, but here's an outline:

Use SwingWorker to manage latency while reading the source data; parse the data in your implementation of doInBackground() and publish() records as they arrive; the first few records will be visible immediately, even though the rest may take some time.

Create a master JTable whose model contains a row for each Record; JTable rendering provides efficient scrolling even for thousands of rows; this table only needs to display enough information for the user to select the desired rows(s).

Add a ListSelectionListener to the master table; in the listener, update an adjacent JTable with details from the master row; use setModel(), illustrated here, to update the detail table.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值