Domanda

I'm adding a string to a JList with DefaultListModel and it takes seconds to appear. Sometimes, I may have to click on the JList to have the list appear.

I'm using Eclipse Indigo. When I set a breakpoint after adding the element to JList, execution is fast.

I searched the web and SO for JList slow and they all talk about adding many items to the list. I'm adding the first element to the list.

Here's my code fragments:

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

I'm also using WindowBuilder with Eclipse.

So how do I improve the performance of JList?

È stato utile?

Soluzione

if you want I can to generating new Item from Char[] and to call doClick() for 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();
            }
        });
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top