title: JList: sorting by Top/Bottom buttons
body: <p>I've got a problem connected with my previous question: <a href="https://stackoverflow.com/questions/10752463/jlist-sorting-by-up-down-buttons">JList: sorting by Up/down buttons</a>
  My Up/Down buttons work properly now, but here's the other thing:
  I need to make Top/Bottom buttons also. They seem to be harder. I use actionPerformed to button "Top":</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   int indexOfSelected = jList1.getSelectedIndex();
   swapElements2(indexOfSelected, 0);
   indexOfSelected = 0;
   jList1.setSelectedIndex(indexOfSelected );
   jList1.updateUI();
  }
  </code></pre>
  
  <p>And swapElements2 method looks like that:</p>
  
  <pre><code>private void swapElements2(int pos1, int pos2) {
    String tmp1 =  (String) listModel.get(pos1);
    String tmp2 =  (String) listModel.get(pos2);
    listModel = (DefaultListModel) jList1.getModel();
    int sizeOfList = listModel.getSize();
  
    listModel.set(pos2, listModel.get(sizeOfList-1));
    for (int i=sizeOfList-2;i&gt;0;i--){
      listModel.set(i+1, listModel.get(i));
    }
  
    listModel.set(pos2, tmp1);
    listModel.set(pos2+1, tmp2);
  }
  </code></pre>
  
  <p>Of course, it works only if the last element of jList is selected. Has anyone an idea how to make an universal method which will move the element from selected position to 0 position? Method should also move all other elements forwards and the last element should stay at it's place if it's not selected.</p>
  
  <p>I'd be grateful for any help. </p>
  
  <p>Edit from comments, which also doesn't work:</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   String temp = (String) listModel.remove(jList1.getSelectedIndex());
   listModel.add(0, temp);
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  name podziwiaj.jpg
  at java.io.File.&lt;init&gt;(File.java:222)
  </code></pre>
  
  <p>Which is:</p>
  
  <pre><code>public File(String pathname) {
  if (pathname == null) {
      throw new NullPointerException();
  }
  this.path = fs.normalize(pathname);
  this.prefixLength = fs.prefixLength(this.path);
  }
  </code></pre>
  
  <p>List stores path names of files (images) and displays is, when item is selected. </p>