Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 987047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:25:02+00:00 2026-05-16T05:25:02+00:00

My application has a module which allows the user to add jButtons on the

  • 0

My application has a module which allows the user to add jButtons on the jLayeredpane during runtime. I want to add action listeners to this dynamically added contents and also i have to provide access to delete the dynamically added buttons during runtime. Is there any way to do this ?

private Map<String, JButton> dynamicButtons;

public void addButton(String name) {
    JButton b = new JButton(name);
    b.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jLayeredPane2.add(b);
    dynamicButtons.put(name, b);
    jLayeredPane2.invalidate();
}

public void removeButton(String name) {
    JButton b = dynamicButtons.remove(name);
    jLayeredPane2.remove(b);
    jLayeredPane2.invalidate();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:25:03+00:00Added an answer on May 16, 2026 at 5:25 am

    Original Answer Good in general, but done differently in this case

    In order to keep track of an arbitrary number of added JButtons, you will need to keep them in a list.

    So, after you create a new button, add the listeners to it, and add it to the pane, you then need to save that new button in a list.

    That way you can keep track of all of the buttons you have added.

    You could also use a Map<String, JButton> that maps a button name to the button.

    Example:

    private Map<String, JButton> dynamicButtons;
    
    public void addButton(String name) {
        JButton b = new JButton(name);
        b.addActionListener(someAction);
        yourPanel.add(b);
        dynamicButtons.put(name, b);
        yourPanel.invalidate();
    }
    
    public void removeButton(String name) {
        Button b = dynamicButtons.remove(name);
        yourPanel.remove(b);
        yourPanel.invalidate();
    }
    

    The following is a full class that lets you add and remove buttons dynamically. It’s not exactly what you want, but it should get you really close.

    Code for your specific case:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class ExampleFrame extends JFrame {
    
        private JButton add, remove;
        private JPanel dynamicButtonPane, addRemovePane;
    
        private boolean waitingForLocationClick;
    
        public ExampleFrame() {
            super("Dynamic button example");
            waitingForLocationClick = false;
            add = new JButton("Add Button");
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    addButton(JOptionPane
                            .showInputDialog("Name of the new button:"));
                }
            });
            remove = new JButton("Remove Button");
            remove.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lookingToRemove = true;
                }
            });
            JPanel mainPane = new JPanel(new BorderLayout());
            dynamicButtonPane = new JPanel();
            dynamicButtonPane.setLayout(null);
            dynamicButtonPane.setPreferredSize(new Dimension(300, 300));
            addRemovePane = new JPanel();
            addRemovePane.add(add);
            addRemovePane.add(remove);
            mainPane.add(dynamicButtonPane, BorderLayout.NORTH);
            mainPane.add(addRemovePane, BorderLayout.SOUTH);
            add(mainPane);
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
            dynamicButtonPane.addMouseListener(pointSelectorListener);
        }
    
        private JButton buttonToPlace;
    
        public void addButton(String name) {
            JButton b = new JButton(name);
            b.setActionCommand(name);
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (lookingToRemove) {
                        if (e.getSource() instanceof JButton) {
                            dynamicButtonPane.remove((Component) e.getSource());
                            dynamicButtonPane.validate();
                            dynamicButtonPane.repaint();
                        }
                    } else
                        JOptionPane.showMessageDialog(ExampleFrame.this, "This is " + e.getActionCommand());
                }
            });
            waitingForLocationClick = true;
            lookingToRemove = false;
            buttonToPlace = b;
        }
    
        public void putButtonAtPoint(Point p) {
            System.out.println("Placing a button at: " + p.toString());
            dynamicButtonPane.add(buttonToPlace);
            buttonToPlace.setBounds(new Rectangle(p, buttonToPlace
                    .getPreferredSize()));
            dynamicButtonPane.validate();
            buttonToPlace = null;
            waitingForLocationClick = false;
        }
    
        private boolean lookingToRemove = false;
    
        private final MouseListener pointSelectorListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (waitingForLocationClick) {
                    putButtonAtPoint(e.getPoint());
                } else {
                    System.out.println("Not in waiting state");
                }
            }
        };
    
        public static void main(String[] args) {
            new ExampleFrame();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.