具有多个JTable的大型JScrollPane-如何分页 [英] Large JScrollPane with multiple JTables—how to paginate

查看:311
本文介绍了具有多个JTable的大型JScrollPane-如何分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Google进行搜索,但是大多数人都提出与大型单表相关的问题,我的问题有所不同.

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.

简而言之,我有一个非常大的文本文件,可以读取并为每行创建对象,例如HeaderRecord或ClaimDetail等.当前文件最终包含约60,000个对象.然后,我为每条记录创建JTable,遍历它们,然后将它们添加到我的滚动窗格中.

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.

我能想到的唯一解决方案是对显示器进行分页.一次只能拉入一定数量的JTables.我不知道该怎么办!如果有人可以帮我一个建议,我会很感激

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

谢谢

更新:我实施了这样的摇摆工人:

Update: I implemented a swingworker like this:

private class TableRun extends SwingWorker<Void, JTable> {
        private ArrayDeque<FileRecord> fileRecords;
        private final GridBagConstraints gc;
        private final JPanel parentPanel;
        int counter = 1;
        TableRun(ArrayDeque<FileRecord> fileRecords, GridBagConstraints gc, JPanel parentPanel) {
            this.fileRecords = fileRecords;
            this.gc = gc;
            this.parentPanel = parentPanel;
        }
        @Override
        protected Void doInBackground() {
            Iterator<FileRecord> iterator = fileRecords.iterator();
            while (iterator.hasNext()) {
                publish(getTabel(iterator.next()));
                Thread.yield();
            }
            return null;
        }
        @Override
        protected void process(final List<JTable> tables) {
                    Iterator<JTable> 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++);
                    }
        }
    }

似乎可以正常工作.我的问题是,在我的JFrame构造函数中,我设置了

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

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

因此,框架在整个屏幕上打开.我想要的是随着swingworker在处理方法中完成将JTable添加到我的面板中后,它应该将那些表添加到面板中并显示它们.因此它应该一直显示,直到完全读取整个文件为止.这不会发生.实际上,在我调整框架大小之前,显示器一直保持空白,然后才显示表格.

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.

我试图从处理方法中调用Outerclass.this.repaint()以便重绘我的Jframe,但这没用...

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

有什么建议吗?

谢谢

推荐答案

超出SO范围的完整设计,但这是一个概述:

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

  • 使用 SwingWorker 进行管理读取源数据时的延迟;在doInBackground()publish()记录的实现中解析到达的数据;前几条记录将立即可见,即使其余记录可能需要一些时间.

  • 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.

创建一个主控JTable,其模型为每个Record包含一行; JTable渲染即使在数千行中也可以提供有效的滚动;该表只需要显示足够的信息供用户选择所需的行.

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).

向其中添加 ListSelectionListener 主表;在侦听器中,使用主行中的详细信息更新相邻的JTable;使用setModel()(在此处所示)更新明细表.

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.

这篇关于具有多个JTable的大型JScrollPane-如何分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆