2012-11-05 3 views
1

JList에 문자열을 추가하고 DefaultListModel으로 표시하는 데 몇 초가 걸립니다. 경우에 따라 목록을 표시하려면 JList을 클릭해야 할 수도 있습니다.JList가 첫 번째 요소를 매우 천천히 추가합니다.

Eclipse Indigo를 사용하고 있습니다. JList에 요소를 추가 한 후 중단 점을 설정하면 실행이 빠릅니다.

나는 웹을 검색하여 JList slow이며 많은 항목을 목록에 추가하는 것에 대해 모두 이야기합니다. 첫 번째 요소를 목록에 추가합니다. 나는 또한 이클립스 WindowBuilder를 사용하고

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

:

여기에 내 코드 조각입니다.

따라서 성능을 어떻게 개선합니까 (JList)?

+2

하십시오'그래서 난의 JList의 성능을 향상 어떻게? '..., 모든 나머지에 뭐죠 귀하의 코드, 항목 (500-2000)의 실수에 대한 performace 문제는 없지만, 100 이상은 소스 쪽의 페이지 매김에 관한 것입니다. – mKorbel

+1

list_functions.setMinimumSize (새 Dimension (32767, 100)); (32767/1080> = 30 FullHD 모니터) WindowBuider가 적절한 방법인지 확인하십시오 (ms_access 또는 wpf와 동일) – mKorbel

답변

2

당신은과에 대한 doClick() 전화를 Char[]에서 생성하는 새로운 Item에 내가 할 수있는 것인지 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(); 
      } 
     }); 
    } 
}