在JTable中动态加载大量数据 [英] Dynamically loading large amount of data in JTable

查看:69
本文介绍了在JTable中动态加载大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我目前有一个JTable,其中包含 5,000到超过200,000行.你知道我要去哪里. 数据已经加载到内存中了,这不是问题,但是如何 我可以创建一个有效的JTable,以便它仅加载那些 是可见的,并且任何事件仅作用于那些 在视口中可见?显然,使用 这么多的数据,因为系统需要永远重新粉刷和发射 事件.

Here is my problem: I currently have a JTable containing anywhere from 5,000 to well over 200,000 rows. You see where I'm going with this. The data is already loaded into memory, which isn't a problem, but how can I create an efficient JTable so that it only loads the rows that are visible and that any events only act on those rows that are visible in the viewport? Obviously scrolling is nearly impossible with this much data, as it takes forever for the system to repaint and fire events.

基本上,我认为一种解决方案是确定哪些行在其中 视口,然后创建一个包含这些行的新模型?

Basically I think one solution would be to determine which rows are in the viewport, then create a new model containing those rows perhaps?

推荐答案

您可以使用 FixedRowsTable 类型设计,这里显示了200,000行.尽管您想添加其他按钮<< >>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class FixedRowsTable {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                String[] columns = {"1","2","3","4","5","6","7"};
                Integer[][] data = new Integer[200000][columns.length];
                for (int xx=0; xx<data.length; xx++) {
                    for (int yy=0; yy<data[0].length; yy++) {
                        data[xx][yy] = new Integer((xx+1)*(yy+1));
                    }
                }
                final int rows = 11;

                JPanel gui = new JPanel(new BorderLayout(3,3));

                final JTable table = new JTable(
                    new DefaultTableModel(data, columns));

                final JScrollPane scrollPane = new JScrollPane(
                    table,
                    JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                Dimension d = table.getPreferredSize();
                scrollPane.setPreferredSize(
                    new Dimension(d.width,table.getRowHeight()*rows));

                JPanel navigation = new JPanel(
                    new FlowLayout(FlowLayout.CENTER));
                JButton next = new JButton(">");
                next.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        int height = table.getRowHeight()*(rows-1);
                        JScrollBar bar = scrollPane.getVerticalScrollBar();
                        bar.setValue( bar.getValue()+height );
                    }
                } );
                JButton previous = new JButton("<");
                previous.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        int height = table.getRowHeight()*(rows-1);
                        JScrollBar bar = scrollPane.getVerticalScrollBar();
                        bar.setValue( bar.getValue()-height );
                    }
                } );

                navigation.add(previous);
                navigation.add(next);

                gui.add(scrollPane, BorderLayout.CENTER);
                gui.add(navigation, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

这篇关于在JTable中动态加载大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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