JList添加第一个元素非常慢

时间:2012-11-05 17:43:12

标签: java swing jlist windowbuilder defaultlistmodel

我正在使用JListDefaultListModel添加字符串,并且需要几秒钟才能显示。有时,我可能需要点击JList才能显示列表。

我正在使用Eclipse Indigo。在将元素添加到JList后设置断点时,执行速度很快。

我在网上搜索了 JList slow ,他们都讨论了在列表中添加很多项目。我正在将第一个元素添加到列表中。

这是我的代码片段:

    private DefaultListModel function_list_model = new DefaultListModel();
    private JList list_functions = new JList(function_list_model);
//...
// Initialization code:
        JPanel panel_function_list = new JPanel();
        panel_function_list.setBounds(10, 53, 541, 220);
        panel_functions.add(panel_function_list);
        panel_function_list.setLayout(null);

        JLabel lblFunctions = new JLabel("Functions");
        lblFunctions.setHorizontalAlignment(SwingConstants.CENTER);
        lblFunctions.setBounds(235, 11, 99, 14);
        panel_function_list.add(lblFunctions);
        list_functions.setBorder(new LineBorder(new Color(0, 0, 0)));

        list_functions.setBounds(10, 42, 492, 177);
        list_functions.setFont(new Font("Courier New", Font.PLAIN, 12));
        list_functions.setPreferredSize(new Dimension(0, 150));
        list_functions.setMinimumSize(new Dimension(32767, 100));
        list_functions.setMaximumSize(new Dimension(32767, 100));
        JScrollPane scrollPane_functions = new JScrollPane(list_functions);
        scrollPane_functions.setBounds(10, 79, 541, 183);
        panel_functions.add(scrollPane_functions);

// Code to add a string:  
    String burger = new String("burger");
    function_list_model.addElement(burger);

我也在使用WindowBuilder和Eclipse。

那么如何提高JList的性能?

1 个答案:

答案 0 :(得分:2)

如果您希望我可以从Item生成新的Char[]并为doClick()致电JButton("Fire")

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.*;

public class ListDemo extends JPanel implements ListSelectionListener {

    private JList list;
    private DefaultListModel listModel;
    private static final String hireString = "Hire";
    private static final String fireString = "Fire";
    private JButton fireButton;
    private JTextField employeeName;
    private javax.swing.Timer timer = null;
    private int delay = 3;
    private int count = 0;

    public ListDemo() {
        super(new BorderLayout());
        listModel = new DefaultListModel();
        listModel.addElement("Jane Doe");
        listModel.addElement("John Smith");
        listModel.addElement("Kathy Green");
        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        list.setVisibleRowCount(5);
        JScrollPane listScrollPane = new JScrollPane(list);
        JButton hireButton = new JButton(hireString);
        HireListener hireListener = new HireListener(hireButton);
        hireButton.setActionCommand(hireString);
        hireButton.addActionListener(hireListener);
        hireButton.setEnabled(false);
        fireButton = new JButton(fireString);
        fireButton.setActionCommand(fireString);
        fireButton.addActionListener(new FireListener());
        employeeName = new JTextField(10);
        employeeName.addActionListener(hireListener);
        employeeName.getDocument().addDocumentListener(hireListener);
        String name = listModel.getElementAt(list.getSelectedIndex()).toString();
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.add(fireButton);
        buttonPane.add(Box.createHorizontalStrut(5));
        buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
        buttonPane.add(Box.createHorizontalStrut(5));
        buttonPane.add(employeeName);
        buttonPane.add(hireButton);
        buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        add(listScrollPane, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.PAGE_END);
        start();
        System.out.println(new Date());
    }

    private void start() {
        timer = new javax.swing.Timer(delay * 10, updateCol());
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                listModel.addElement("Jane Doe");
                listModel.addElement("John Smith");
                listModel.addElement("Kathy Green");
                list.ensureIndexIsVisible(listModel.getSize()-1);
                count++;
                if (count >= 500) {
                    timer.stop();
                    System.out.println("update cycle completed");
                    System.out.println(new Date());
                }
            }
        };
    }

    class FireListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int index = list.getSelectedIndex();
            listModel.remove(index);
            int size = listModel.getSize();
            if (size == 0) { //Nobody's left, disable firing.
                fireButton.setEnabled(false);
            } else { //Select an index.
                if (index == listModel.getSize()) {
                    index--;
                }
                list.setSelectedIndex(index);
                list.ensureIndexIsVisible(index);
            }
        }
    }

    class HireListener implements ActionListener, DocumentListener {

        private boolean alreadyEnabled = false;
        private JButton button;

        HireListener(JButton button) {
            this.button = button;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = employeeName.getText();
            if (name.isEmpty() || alreadyInList(name)) {
                Toolkit.getDefaultToolkit().beep();
                employeeName.requestFocusInWindow();
                employeeName.selectAll();
                return;
            }
            int index = list.getSelectedIndex(); //get selected index
            if (index == -1) { //no selection, so insert at beginning
                index = 0;
            } else {           //add after the selected item
                index++;
            }
            listModel.insertElementAt(employeeName.getText(), index);
            //listModel.addElement(employeeName.getText());
            employeeName.requestFocusInWindow();
            employeeName.setText("");
            list.setSelectedIndex(index);
            list.ensureIndexIsVisible(index);
        }

        protected boolean alreadyInList(String name) {
            return listModel.contains(name);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            enableButton();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            handleEmptyTextField(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            if (!handleEmptyTextField(e)) {
                enableButton();
            }
        }

        private void enableButton() {
            if (!alreadyEnabled) {
                button.setEnabled(true);
            }
        }

        private boolean handleEmptyTextField(DocumentEvent e) {
            if (e.getDocument().getLength() <= 0) {
                button.setEnabled(false);
                alreadyEnabled = false;
                return true;
            }
            return false;
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() == false) {

            if (list.getSelectedIndex() == -1) {
                fireButton.setEnabled(false);
            } else {
                fireButton.setEnabled(true);
            }
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ListDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new ListDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}