This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Use the remove function
Posted by Hiran on February 15, 2002 at 11:12 AM
JFrame has a remove function in addition to the add function. Check the Java documentation, but I believe it works the same way. You just call remove and specify the component to remove. Hiran > Hello does anyone knwo how to delete a button? > For example consider the following senario - > You have a JFrame, JPanel and a JToolbar attached to the frame > and on the toolbar you have 2 buttons , one when clicked adds a button to the JPanel. The button that is added can move around and if you press the button again another the button is added. > The second button deletes the selected button. > Below I have included the code that addes the button and stuff. Does anyone know how to complete the code that deletes a the selected button on the JPanel? Any help would be greatly appreciated... thanks... > Here is the code --> > import java.awt.*; > import java.awt.event.*; > import javax.swing.*; > import javax.swing.event.*; > import java.util.*; > import java.awt.event.MouseListener; > import java.awt.event.MouseEvent; > import java.awt.event.MouseMotionListener; > class DeleteButton > { > public static int i =0 ; > > public static void main(String[] args) > { > > JFrame frame = new JFrame() ; > final JPanel pane = new JPanel(); > pane.setLayout(null); > > JToolBar tool = new JToolBar(); > JButton addButton = new JButton("Add"); > JButton deleteButton = new JButton("Delete"); > > addButton.addActionListener(new ActionListener(){ > public void actionPerformed(ActionEvent e){ > > JButton button = new JButton("" + i); > pane.add(button); > button.setBounds(10,90,80,50); > button.addMouseMotionListener(new MouseMotionAdapter() { > public void mouseDragged(MouseEvent me) { > me.getComponent().setLocation(me.getComponent().getX() + me.getX(), > me.getComponent().getY() + me.getY()); > > } > }); > > i++ ; > > } > }); > > tool.add(addButton); > tool.add(deleteButton); > pane.updateUI(); > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > frame.getContentPane().add(pane); > frame.getContentPane().add("North", tool); > frame.setSize(500,500); > frame.setVisible(true); > > } > }
Replies:
|