Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to delete JButton on JPanel dynamically?

Posted on 2002-05-11
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

raid999 earned 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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