title: problems revalidating/repainting button panel
body: <p>I've been googling for hours trying a million different subtle changes all to no avail, so I figure my best bet is to pick the brains of people more skilled than I.</p>
  
  <p>I'm writing a class that loads a bunch of buttons from a database and the goal is to offer the user the ability to arrange the buttons to their liking, however, for some reason, I can't seem to get the frame to revalidate or repaint. The buttons will move around, but they will not rearrange as I have coded them to. The organization seems to be working correctly, ie the code involved in making the re arrangement when the mouse button is released, its just that the components will only stay where they are dragged and dropped, even though they are re ordered in their respective List. </p>
  
  <p>The code is long, I didn't want to have to post the whole class as it may turn some people off but I don't know where I'm making the mistake so I think it would be in my best interest to post the whole thing. The main area of concern is <code>mouseReleased(MouseEvent e) {...}</code> and the <code>repaint()</code>/<code>refresh()</code> method, however, there could be something that I'm missing elsewhere. </p>
  
  <p>tl;dr: 
  I'm basically just trying to perform a <code>setBounds()</code> after the user drags and drops the buttons in the order they want but the buttons stay in the same spot they are dragged and dropped, and won't <code>revalidate()</code> or <code>repaint()</code>. I can't even <code>removeAll</code> to clear the panel of components and reload.</p>
  
  <p>Thank you in advanced. Here's my code:</p>
  
  <pre><code>public class AdminButtonEditor extends javax.swing.JFrame {
      public AdminButtonEditor(OrderView parent) { ...
          ...
          Component[] components = buttonsPanel.getComponents();
          for (int i = 0; i &lt; components.length; i++) {
              Component c = components[i];
              if (c instanceof JButton) {
                  JButton jb = (JButton) c;
                  jb.setFocusable(false);
                  buttons.add(new MyJButton(...));
              }
          }
          for (int i = 0; i &lt; buttons.size(); i++) {
              buttons.get(i).addTo(editPanel);
              buttons.get(i).orderIndex=modButtonList.get(i).menuModifier.getViewOrderValue();
              buttons.get(i).idx=i;
          }
          EventHandler eh = new EventHandler();
          addWindowListener(eh);
          editPanel.addMouseMotionListener(eh);
          editPanel.addMouseListener(eh);
          contentPane.add(editPanel, BorderLayout.CENTER);
      }
      protected void refresh() {
          if (!buttons.isEmpty() &amp;&amp; buttons.get(0) != null) {
              contentPane.remove(editPanel);
              editPanel.removeAll();
              for (int i = 0; i &lt; buttons.size(); i++) {
                  MyJButton s = buttons.get(i);
                  s.addTo(editPanel);
              }
              contentPane.add(editPanel, BorderLayout.CENTER);
              editPanel.repaint();
          }
      }
      public void paint(Graphics g) {
         refresh();
         super.paint(g);
      }
      private int getSelectionIndex(int x, int y) {
          int s=-1;
          for (int i=buttons.size()-1; i&gt;=0;i--){
              if (buttons.get(i).contains(x, y)) {
                  s = i;
                  break;
              }
          }
          return s;
      }
  private class EventHandler implements MouseInputListener,WindowListener, ActionListener {
      private int selectionIndex, startX, startY, lastX, lastY;
      private MyJButton selected;
      private boolean moving=false;
      ....
      ....
      public void mouseReleased(MouseEvent e) {
          if (moving){
              setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
              moving=false;
              int dropX=e.getX();
              int dropY=e.getY();
              int row = dropY/selected.height;
              int col = dropX/selected.width;
              int idx=(row*3+col)-1;
              int oldIdx=buttons.indexOf(selected);
              insertIntoList(idx,oldIdx);
           }
          if (selected!=null){
              selected.unHighLight();
              selected=null;
          }
  
          Collections.sort(buttons);
          for (int i=0;i&lt;buttons.size();i++){
          //modButtonList.get(buttons.get(i).idx).menuModifier.setViewOrderValue(buttons.get(i).orderIndex);
          }
          editPanel.validate();
          repaint();
      }
  
      private void insertIntoList(int idx, int oldIdx) {
          MyJButton temp = buttons.get(idx);
          int tempid=buttons.get(idx).idx;
          buttons.set(idx, new MyJButton(selected.text,selected.x,selected.y,selected.width,selected.height,selected.idx));
          buttons.get(idx).orderIndex=temp.orderIndex;
          if (idx&lt;oldIdx){
              int id;
              for (int i=oldIdx;i&gt;idx+1;i--){
                  id=buttons.get(i).orderIndex;
                  buttons.set(i, new MyJButton(buttons.get(i-1).text,buttons.get(i-1).x,buttons.get(i-1).y,buttons.get(i-1).width,buttons.get(i-1).height,buttons.get(i-1).idx));
                  buttons.get(i).orderIndex=id;
              }
              id = buttons.get(idx+1).orderIndex;
              buttons.set(idx+1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx));
              buttons.get(idx+1).orderIndex=id;
          } else if (idx&gt;oldIdx) {
              int id;
              for (int i=oldIdx;i&lt;idx-1;i++){
                  id=buttons.get(i).orderIndex;
                  buttons.set(i, new MyJButton(buttons.get(i+1).text,buttons.get(i+1).x,buttons.get(i+1).y,buttons.get(i+1).width,buttons.get(i+1).height,buttons.get(i+1).idx));
                  buttons.get(i).orderIndex=id;
              }
              id = buttons.get(idx-1).orderIndex;
              buttons.set(idx-1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx));
              buttons.get(idx-1).orderIndex=id;;
          } else {
              buttons.get(idx).x=buttons.get(idx).originx;
              buttons.get(idx).y=buttons.get(idx).originy;
          }
          repaint();
      }
      public void mouseDragged(MouseEvent e) {
          if (moving) {
              Graphics g = editPanel.getGraphics();
              g.setColor(Color.black);
              g.drawLine(selected.getXPos(), 0, selected.getXPos(),editPanel.getWidth());
              g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos());
              selected.moveBy(e.getX()-lastX, e.getY()-lastY);
              g.setXORMode(Color.black);
  
              g.drawLine(selected.getXPos(), 0, selected.getXPos(), editPanel.getWidth());
              g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos());
  
              lastX=e.getX();
              lastY=e.getY();
              repaint();
          }
      }
      ....
  }
    private class MyJButton extends JButton implements Comparable {
      private int orderIndex,idx;
      private int x, y, width, height,originx,originy;
      private String text;
      public Border DEFAULT_BORDER;// new SoftBevelBorder(BevelBorder.RAISED);
      public Border SELECT_BORDER = BorderFactory.createLineBorder(Color.RED, 3, true);
  
      public MyJButton(String text, int x, int y, int width, int height) {
          ....
          setFocusable(false);
      }
      public MyJButton(String text, int x, int y, int width, int height,int idx) {....}
  
      public void addTo(JPanel p) {
          setBounds(x, y, width, height);
          p.add(this);
      }
  
      @Override
      public boolean contains(int x, int y) {
          int x1 = x, y1 = y;
          if (x1 &gt;= this.x &amp;&amp; y1 &gt;= this.y &amp;&amp; x1 &lt;= this.x + width &amp;&amp; y1 &lt;= this.y + height) {
              return true;
          } else {
              return false;
          }
      }
  
      @Override
      public void setSize(int w, int h) {
          width = w;
          height = h;
      }
      ....
      public void moveBy(int dx, int dy) {
          x += dx;
          y += dy;
      }
  
      @Override
      public void resize(int newWidth, int newHeight) {
          this.width = newWidth;
          this.height = newHeight;
          setBounds(x, y, width, height);
      }
  
      public int compareTo(Object o) {
          MyJButton mjb = (MyJButton)o;
          return this.idx-mjb.idx;
      }
  
    }
  }
  </code></pre>