title: Create a custom list in Swing Java
body: <p>I want to create a custom list using JLabels in Swing. (Much like VLC playlist). The reason I chose JLabels instead of a JTable or List is that it allows more customizing like imageIcons easily. </p>
  
  <p>But now I have a problem. I want to be able to move the labels up and down by dragging(Changing the row order). The JLables are in a boxlayout to be viewed as a list. Can someone help me with this?
  Can I repaint the jPanel after setting the locations of Jlabels?</p>
  
  <pre><code>public class playListPanel extends JPanel {
  private mediaList mdList;
  private JPanel base;
  private JScrollPane scrollPane;
  private boolean locked = false;
  private JLabel lockedlb ;
  public playListPanel(mediaList mdList){
  
  
      this.mdList = mdList;
      this.setLayout(new BorderLayout());
        init();  
       this.addMouseListener(new MouseAdapter(){
           public void mouseMoved(MouseEvent evt){
                 if(locked){
                  lockedlb.setLocation(evt.getPoint());
                  base.repaint();
                 }
              }
              public void mouseDragged(MouseEvent evt){
                 lockedlb.setLocation(evt.getPoint());
                 base.repaint();
  
              }
              public void mouseRelease(MouseEvent evt){
                 if(locked){
                 int y = evt.getY();
                 int n = y%20;
                 mediaList temp = (mediaList) mdList.copy();
                 temp.remove(Integer.getInteger(lockedlb.getName())-1);
                   temp.insert(mdList.get(Integer.getInteger(lockedlb.getName())-1),n);
                 mdList.set(temp);
                 init();
                 base.repaint();
                 locked = true;
                 }else{
                 } 
              }
  
      });
  }
  
  public void init(){
       base = new JPanel();
      base.setPreferredSize(new Dimension(300,0));
      scrollPane = new JScrollPane(base);
  
      base.setLayout(new BoxLayout(base,BoxLayout.Y_AXIS));
  
      int i= 0;
      for(Object m : this.mdList){
          i = i+1;
          media temp = (media)m;
          JLabel label = new JLabel(" "+ temp.getMediaTitle());
          label.setName("" +i);
          label.setMaximumSize(new Dimension(label.getPreferredSize().width,label.getPreferredSize().height+4 ));
          label.addMouseListener(new MouseAdapter(){
  
              public void mousePressed(MouseEvent evt){
                  locked =true;
                  lockedlb = label;
                  //label.setLocation(evt.getPoint());
              }
  
              public void mouseMoved(MouseEvent evt){
                 if(locked){
                  lockedlb.setLocation(evt.getPoint());
                  base.repaint();
                 }
              }
              public void mouseDragged(MouseEvent evt){
                 lockedlb.setLocation(evt.getPoint());
                 base.repaint();
  
              }
              public void mouseRelease(MouseEvent evt){
                 if(locked){
                 int y = evt.getY();
                 int n = y%20;
                 mediaList temp = (mediaList) mdList.copy();
                 temp.remove(Integer.getInteger(lockedlb.getName())-1);
                 temp.insert(mdList.get(Integer.getInteger(lockedlb.getName())-1),n);
                 mdList = temp;
                 init();
                 base.repaint();
                 locked = true;
                 }else{
                 }
                 }
  
          }
          );
          base.add(label);
          this.add(base,BorderLayout.CENTER);
      }
  }
  }
  </code></pre>