简体   繁体   中英

How do I remove a button in one class/component using the actionPerformed method of another class?

In the code I am designing, I have a JButton instantiated in the main JFrame of my application that is using an ActionListener referencing an actionPerformed method in another class/component. I'm wondering if there is a way to remove that button upon running said actionPerformed method (clicking on it). In other words, is it possible for the actionPerformed method to reference back to the button that is activating it?

Can you get a reference to the object that caused the ActionListener to activate? Yes via the ActionEvent parameter's getSource() method.

ie,

public void actionPerformed(ActionEvent evt) {
   Object theSource = evt.getSource();
}

A warning though: if you use this ActionListener or Action in multiple settings, such as with menus, the source may not be a button at all.

As far as using this to "remove" the button, you would then need to get the component's container, something like:

public void actionPerformed(ActionEvent evt) {
   JButton button = (JButton) evt.getSource(); // danger when casting!
   Container parent = button.getParent();
   parent.remove(button);
   parent.revalidate();
   parent.repaint();
}

but again, I have to warn that you'll be in trouble if the event was not caused by a JButton, or if the layout manager that the parent uses doesn't things being removed. I have to wonder if a CardLayout would be better to use here.


eg,

import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class RemoveButton extends JPanel {
   private static final long serialVersionUID = 1L;
   private static final int BUTTON_COUNT = 5;
   private Action removeMeAction = new RemoveMeAction("Remove Me");
   public RemoveButton() {
      for (int i = 0; i < BUTTON_COUNT; i++) {
         add(new JButton(removeMeAction));
      }
   }

   private static void createAndShowGui() {
      RemoveButton mainPanel = new RemoveButton();

      JFrame frame = new JFrame("Remove Buttons");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class RemoveMeAction extends AbstractAction {
   private static final long serialVersionUID = 1L;

   public RemoveMeAction(String name) {
      super(name); // to set button's text and actionCommand
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      Object source = evt.getSource();

      // AbstractButton is the parent class of JButton and others
      if (source instanceof AbstractButton) {
         AbstractButton button = (AbstractButton) source;
         Container parent = button.getParent();

         parent.remove(button);
         parent.revalidate();
         parent.repaint();
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM