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

  • SEARCH
  • Home
  • 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 9039561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:45:34+00:00 2026-06-16T09:45:34+00:00

why doesnt the following code work? Basically, this is a simplified version of a

  • 0

why doesnt the following code work? Basically, this is a simplified version of a more difficult program in which I am trying to make an runnable initial screen with selections that would then have buttons that link to different runnables, but this doesn’t run as I expected it to.

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

public class Runnables {
    static Runnable runone;
    static Runnable runtwo;
    static JFrame frame = new JFrame();
    static JButton button1 = new JButton("Initial screen");
    static JButton button2 = new JButton("After button click screen");

    public static void main(String[] args) {
        runone = new Runnable() {
            @Override
            public void run() {
                frame.removeAll();
                frame.revalidate();
                frame.repaint();
                frame.add(button2);
            }

        };
        runtwo = new Runnable() {
            @Override
            public void run() {
                frame.setSize(800, 600);
                frame.setVisible(true);
                button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        runone.run();
                        System.out
                                .println("This is performed, but the button doesnt change");
                    }
                });
                frame.add(button1);
            }
        };
        runtwo.run();
    }
}
  • 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-16T09:45:35+00:00Added an answer on June 16, 2026 at 9:45 am

    There’s nothing special about Runnable that would prevent this from working. As your code sample stands, the following are equivalent:

    public void actionPerformed(ActionEvent arg0) {
        runone.run();
        System.out.println("This is performed, but the button doesnt change");
    }
    

    and

    public void actionPerformed(ActionEvent arg0) {
        frame.removeAll();
        frame.revalidate();
        frame.repaint();
        frame.add(button2);
        System.out.println("This is performed, but the button doesnt change");
    }
    

    Taking your code and adding a System.out.println debug statement inside runone.run shows that it is in fact being executed.

    I assume your code sample is meant to be a simplified demo of your issue; you may want to look into getting it to do what you want as a ‘plain function’ first (my second example above, where the Runnables are combined), then separating into distinct Runnables afterwards.

    Edit – to get your example to work, the thing to remember is that JFrame uses a contentPane to host its children – frame.add exists as a convenience to add to the contentPane (based on javadoc for JFrame), but removeAll does not do this (based on me playing with it just now). Also, calling validate after the button is added will properly relayout the subcomponents again to get your second button to appear.

    Replace your definition of runone with this one, and your sample will work:

    runone = new Runnable() {
        @Override
        public void run() {
            frame.getContentPane().removeAll();
            frame.add(button2);
            frame.validate();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: http://pastebin.com/EgjbzqA2 which is basically just a stripped down version
I am trying to get the following to work. I have this code in
I have the following code but it doesnt seem to work: <td align=center style=padding-bottom:52.5px>
The following code doesn't work! 'this' in in the context when i access it
i have the following code from my JSP which doesn't quite work: <%@ taglib
I have the following code on my website, which basically checks to see if
All, I've got the following code to which is basically a function that loads
Why doesn't following code work correctly in FireFox 3.6? I have tested in IE7,
Why doesn't the following code work? MyViewController *viewController = [[MyViewController alloc] init]; [myWindow addSubview:viewController.view];
The following code doesn't work (of course), because the marked line does not compile:

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.