How to delete JButton on JPanel dynamically?

Posted on 2002-05-11
Last Modified: 2010-05-18
   I dynamically add several JButton Objects in a ArrayList by other codes. After that,what  I want to implement is :When a JButton on the JPanel is clicked, the second element in the arraylist will be removed. Then I repaint the other elements on JPanel, the second JButton should disappeare,  but the code as following does not work.
   Would you show me the right codes? Thank you in advance!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class DelJComponent extends JApplet {
    JP jp=new JP();
    ArrayList StepCollection=new ArrayList();
     public void init()
          for (int i=0;i<3;i++)
               {   StepNode stepObj=new StepNode();
     class JP extends JPanel
          public void paint(Graphics g)
               for (int i=0;i<StepCollection.size();i++)
                      StepNode tempObj=(StepNode)StepCollection.get(i);
     class StepNode extends JButton
               setSize(new Dimension(30,20));
               addMouseListener(new StepMouseProcess());
          public void delStep()
               StepCollection.remove(2);//2 is for test
         private class StepMouseProcess extends MouseAdapter
               public void mouseClicked(MouseEvent e)



Question by:CFree
  • 2

Accepted Solution

raid999 earned 50 total points
ID: 7003600
I have done a similar programe
Have a look at this
and then download the zip file.

Author Comment

ID: 7003837
raid999 : there is some difference between our delete the JButton by JPanel's mumber function "remove",but I want to delete the element in ArrayList then show the left on the JPanel.

Expert Comment

ID: 7004250
right sorry mate i will try to think of your way and if i come to any thing i will let you know.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 39
Android development question 2 75
Crystal Reports Licensing Questions 4 56
Html split(text) 2 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question