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 8296977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:13:52+00:00 2026-06-08T15:13:52+00:00

I always have trouble with this when coding my Swing applications, I figured I’d

  • 0

I always have trouble with this when coding my Swing applications, I figured I’d finally get a definative answer instead of playing around with it until I get it working…

I have a JFrame. Inside this JFrame is a JButton. In the ActionListener I want to almost empty the JFrame leaving one or two components (which would include removing the JButton). The application then freezes because you can’t remove the component until it’s ActionListener finishes. How do I get around that?

  • 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-06-08T15:13:53+00:00Added an answer on June 8, 2026 at 3:13 pm

    Don’t forget to call validate() and repaint() on the container when you’re removing components and should work OK.

    import java.awt.Component;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class RemoveDemo {
    
        static class RemoveAction extends AbstractAction{
            private Container container;
    
            public RemoveAction(Container container){
                super("Remove me");
                this.container = container;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                container.remove((Component) e.getSource());
                container.validate();
                container.repaint();    
            }
        }
    
        private static void createAndShowGUI() {
            final JFrame frame = new JFrame("Demo");
            frame.setLayout(new FlowLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            RemoveAction action = new RemoveAction(frame);
            frame.add(new JButton(action));
            frame.add(new JButton(action));
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always have trouble with complicated SQL queries. This is what I have $query
I have trouble figuring out why a simple division like this one always returns
I always have trouble mixing languages and I recently started with MYSQL. In this
I always have trouble designing the user interface when it come to manage a
I'm having a bit of trouble passing this parameter to a class i have.
whan i use img element in anchor I always have a trouble with blank
well i have this trouble and ive been dealing with but i just cant
I always have trouble with Java layouts, but the main thing bugging me now
why do I always have so much trouble with the model binder?? I have
I'm having trouble with this and, from the research I have done, I think

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.