title: Removing dynamically Objects from JPanel
body: <p>Via a <code>JSpinner</code> I want to add and remove object "porta" from a<code>JPanel</code>, it works fine for adding and when I remove only one object if I try to remove two or more objects it doesn't work properly. This is the code I think the issue is related to the last element of the <code>vector</code> on which I add the object "porta":</p>
  
  <pre><code> import javax.swing.JFrame;
  
  public class PreparaGara extends JFrame {
  
  public JSpinner spinner;
  private JPanel panel_1,panel_2;
  private int spin;
  private int NPorta = 1;
  private PortaP scorriPorta,porta;
  final Vector&lt;PortaP&gt; vp = new Vector&lt;PortaP&gt;();
  
  /**
   * Launch the application.
   */
  
  /**
   * Create the application.
   */
  public PreparaGara() {
  
      initialize();
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
  
      final JFrame frame = new JFrame();
      frame.setVisible(true);
      frame.setBounds(100, 100, 1000, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(new GridLayout(4, 1, 3, 3));
  
  
      JMenuBar menuBar = new JMenuBar();
      frame.getContentPane().add(menuBar);
  
      JMenu mnGara = new JMenu("Gara");
      mnGara.setFont(new Font("Courier New", Font.BOLD, 30));
      menuBar.add(mnGara);
  
      panel_1 = new JPanel();
      frame.getContentPane().add(panel_1);
      panel_1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
  
      final JPanel panel_2 = new JPanel();
      frame.getContentPane().add(panel_2);
      panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
  
  
      JMenuItem mntmPorte = new JMenuItem("Porte");
      mntmPorte.setFont(new Font("Segoe UI", Font.PLAIN, 20));
      mntmPorte.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
  
  
              JLabel lbl = new JLabel();
              lbl.setText("Numero Porte : ");
              panel_1.add(lbl);
  
  
              SpinnerNumberModel sm =new SpinnerNumberModel(0,0,30,1);
              final JSpinner spinner = new JSpinner(sm);
              panel_1.add(spinner);       
              spinner.getFocusListeners();
              spin=0;
              panel_1.revalidate();
  
              spinner.addChangeListener(new ChangeListener() {
  
                  @Override
                  public void stateChanged(ChangeEvent arg0) {
                      int ora=(int) spinner.getValue();
  
                      if(spin&lt;ora &amp;&amp; spin&lt;30){
  
                          porta = new PortaP(NPorta);
                          panel_2.add(porta);
                          NPorta++;
                          vp.add(porta);
                          panel_2.revalidate();
  
  
                      }else{
                          //int i = vp.size();
                          panel_2.remove(vp.lastElement());
                          //panel_2.remove(porta);
                          vp.lastElement().remove(porta);                         
                          NPorta=NPorta-1;
                          //System.out.println(i);
                          panel_2.revalidate();
                          panel_2.repaint();
                      }
  
                  spin=ora;
                  }});
          }
      });
      mnGara.add(mntmPorte);
  
      JMenuItem mntmGiudici = new JMenuItem("Giudici");
      mntmGiudici.setFont(new Font("Segoe UI", Font.PLAIN, 20));
      mnGara.add(mntmGiudici);
  
      JMenu mnDbase = new JMenu("Dbase");
      mnDbase.setFont(new Font("Courier New", Font.BOLD, 30));
      menuBar.add(mnDbase);
  
      JMenuItem mntmResetPenalit = new JMenuItem("Reset Penalit\u00E0");
      mnDbase.add(mntmResetPenalit);
  
      JMenuItem mntmResetPorte = new JMenuItem("Reset Porte");
      mnDbase.add(mntmResetPorte);
  
      JMenuItem mntmRestGiudici = new JMenuItem("Reset Giudici");
      mntmRestGiudici.setActionCommand("Reset Giudici");
      mnDbase.add(mntmRestGiudici);
  
      JMenu mnReturn = new JMenu("Return");
      mnReturn.setFont(new Font("Courier New", Font.BOLD, 30));
      menuBar.add(mnReturn);
  
      JMenuItem mntmChiudiMenu = new JMenuItem("Chiudi Menu");
      mnReturn.add(mntmChiudiMenu);
  
  
  
  
  
  
      JPanel panel = new JPanel();
      frame.getContentPane().add(panel);
    }
  
   }
  </code></pre>