Solved

How to delete JButton on JPanel dynamically?

Posted on 2002-05-11
3
2,369 Views
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()
     {
         getContentPane().add(jp);
          for (int i=0;i<3;i++)
               {   StepNode stepObj=new StepNode();
                    StepCollection.add(stepObj);
               }
         
     }
     class JP extends JPanel
     {
          JP()
          {
          }
          public void paint(Graphics g)
          {
               super.paint(g);
               for (int i=0;i<StepCollection.size();i++)
                    {
                      StepNode tempObj=(StepNode)StepCollection.get(i);
                        add(tempObj);
                        tempObj.setLocation(25*i+50,25*i+50);
                    }
               g.drawString(Integer.toString(StepCollection.size()),30,10);
          }
     }    
     class StepNode extends JButton
     {
          StepNode()
          {
               setSize(new Dimension(30,20));
               addMouseListener(new StepMouseProcess());
          }
          public void delStep()
          {
               StepCollection.remove(2);//2 is for test
               jp.validate();
               jp.repaint();
          }
         private class StepMouseProcess extends MouseAdapter
         {
               public void mouseClicked(MouseEvent e)
               {
                        delStep();
                 }
            }          

     }

}

0
Comment
Question by:CFree
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
raid999 earned 50 total points
Comment Utility
I have done a similar programe
Have a look at this
ftp://80.195.36.75/Raid/MSc/JAVA/Resturant
and then download the zip file.
0
 

Author Comment

by:CFree
Comment Utility
raid999 : there is some difference between our codes.you 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.
0
 
LVL 1

Expert Comment

by:raid999
Comment Utility
right sorry mate i will try to think of your way and if i come to any thing i will let you know.

_________________________
cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now