当前位置:  开发笔记 > 编程语言 > 正文

Swing中使用AbstractListModel的大型虚拟列表 - 限制为119,000,000个项目?

如何解决《Swing中使用AbstractListModel的大型虚拟列表-限制为119,000,000个项目?》经验,为你挑选了1个好方法。

我的摇摆工作中的"虚拟列表"运行良好,但是当我超过一些特定数量的项目时它似乎失败了."失败"是指当项目数量> Nmax时滚动条神奇地消失,当项目数量<= Nmax时它会返回; Nmax似乎在我的系统上大约119,304,000.

我跑来跑去了什么?!?!

(这是一个测试程序:在我的电脑上,如果输入119,304,它可以正常工作,但我点击向上箭头,滚动条消失了)

package com.example.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// based on:
// http://www.java2s.com/Tutorial/Java/0240__Swing/extendsAbstractListModel.htm
// http://www.java2s.com/Tutorial/Java/0240__Swing/SpinnerNumberModel.htm
// http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SpinnerNumberModel.html
// http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningforJSpinnerEventswithaChangeListener.htm
// http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html

public class BigVirtualList extends JFrame {

    public static void main(String[] args) {
        new BigVirtualList();
    }

    static final int initialLength = 1;
    final private JList list1 = new JList();
    final private BVLData bvldata = new BVLData(initialLength*1000);

    public BigVirtualList() {
        this.setTitle("Big virtual list");
        this.getContentPane().setLayout(new BorderLayout());
        this.setSize(new Dimension(400, 300));
        list1.setModel(bvldata);

        list1.setPrototypeCellValue(list1.getModel().getElementAt(0));


        SpinnerModel model1 = new SpinnerNumberModel(initialLength,1,1000000,1);
        final JSpinner spinner1 = new JSpinner(model1);

        this.getContentPane().add(new JScrollPane(list1), BorderLayout.CENTER);
        JLabel label1 = new JLabel("Length (1000s of items):");
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.add(label1, BorderLayout.WEST);
        panel1.add(spinner1, BorderLayout.CENTER);
        this.getContentPane().add(panel1, BorderLayout.SOUTH);      

        ChangeListener listener = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                Integer newLength = (Integer)spinner1.getValue();
                bvldata.setLength(newLength*1000);
            }
        };

        spinner1.addChangeListener(listener);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

class BVLData extends AbstractListModel {
    public BVLData(int length) { this.length = length; }

    private int length;

    public int getLength() { return length; }
    public void setLength(int length) {
        int oldLength = getLength();
        this.length = length;
        int newLength = getLength();

        if (newLength > oldLength)
            fireIntervalAdded(this, oldLength+1, newLength);
        else if (newLength < oldLength)
            fireIntervalRemoved(this, newLength+1, oldLength);      
    }   

    @Override
    public Object getElementAt(int index) {
        return "Item "+index+" mod 107 = "+(index%107);
    }

    @Override
    public int getSize() { return getLength(); }

}

小智.. 6

好吧,我没有看到目的,但无论如何......

我猜你在滚动窗格的某个地方打破了整数最大值:

System.out.println("CellHeight:"+list1.getFixedCellHeight());
System.out.println("CellHeight*119304000:"+NumberFormat.getNumberInstance().format(list1.getFixedCellHeight()*119304000.0));
System.out.println("MAXINT:"+Integer.MAX_VALUE);

给你数字:

CellHeight:18
CellHeight*119304000:2.147.472.000
MAXINT:2.147.483.647

正如你所看到的,在高度上增加另外18000像素将超出MAXINT ......这会让你遇到麻烦.



1> 小智..:

好吧,我没有看到目的,但无论如何......

我猜你在滚动窗格的某个地方打破了整数最大值:

System.out.println("CellHeight:"+list1.getFixedCellHeight());
System.out.println("CellHeight*119304000:"+NumberFormat.getNumberInstance().format(list1.getFixedCellHeight()*119304000.0));
System.out.println("MAXINT:"+Integer.MAX_VALUE);

给你数字:

CellHeight:18
CellHeight*119304000:2.147.472.000
MAXINT:2.147.483.647

正如你所看到的,在高度上增加另外18000像素将超出MAXINT ......这会让你遇到麻烦.

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有