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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:55:21+00:00 2026-06-16T00:55:21+00:00

Recently I’ve attempted to make a simple program that would require moving a button

  • 0

Recently I’ve attempted to make a simple program that would require moving a button across the screen several times, but in order to do this I have to be able to access the JPanel from certain parts of the code which I don’t seem to be able to do, or find an alternative method. Here is a small program that should pinpoint the problems I’m having.

public class ButtonMover extends JFrame
{

    public static void main(String[] args) {

        new ButtonMover();
    }
        JButton actionButton;

        public ButtonMover() {
            JPanel buttonMoverPanel = new JPanel();
            buttonMoverPanel.setLayout(new GridBagLayout());
            this.add(buttonMoverPanel);
            this.setSize(500,500);
            this.setResizable(true);
            this.setVisible(true);

            JButton actionButton = new JButton("Testing Button");
            buttonMoverPanel.add(actionButton);

            ClickListener c = new ClickListener();

            actionButton.addActionListener(c);

        }

        private class ClickListener
                    implements ActionListener
           {
               public void actionPerformed(ActionEvent e)
               {

                       if (e.getSource() == actionButton)
                            buttonMoverPanel.add(new JLabel("Testing Label"));
                            //insert code to move button here
               }
           }
}

The |buttonMoverPanel.add(new JLabel(“Testing Label”));| line is the only part that doesn’t work, because I seem unable to make a reference to buttonMoverPanel from that area. While it doesn’t actually cause any errors, it prevents actionButton from doing anything.

  • 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-16T00:55:22+00:00Added an answer on June 16, 2026 at 12:55 am

    If you need to access a variable, here your buttonMoverPanel, then don’t hide it by declaring it in a method or constructor making it only visible in that method or constructor. No, declare it in the class so that it is visible throughout the class.

    So again, one improvement for this code is to declare the buttonMoverPanel in the class just the same as you’re currently doing with your actionButton JButton.

    Edit: you’re shadowing your actionButton variable — you’re re-declaring it in the constructor, so that the button added to the GUI is not referred to by the actionButton class field. Don’t re-declare it in the class.

    In other words the line indicated creates a completely new actionButton variable, one that is only visible in the constructor:

    JButton actionButton;
    JPanel buttonMoverPanel = new JPanel();
    
    public ButtonMover() {
      buttonMoverPanel.setLayout(new GridBagLayout());
      this.add(buttonMoverPanel);
      this.setSize(500, 500);
      this.setResizable(true);
      this.setVisible(true);
    
      JButton actionButton = new JButton("Testing Button"); // ****** here
    

    The solution is to not re-declare the variable to rather to use the class field:

    JButton actionButton;
    JPanel buttonMoverPanel = new JPanel();
    
    public ButtonMover() {
      buttonMoverPanel.setLayout(new GridBagLayout());
      this.add(buttonMoverPanel);
      this.setSize(500, 500);
      this.setResizable(true);
      this.setVisible(true);
    
      actionButton = new JButton("Testing Button"); // ****** Note the difference???
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently (that is in winter in few days) I wrote a simple script which
Recently, I was writing a class in which I discovered that I could reduce
recently, while working on a db2 -> oracle migration project, we came across this
Recently, I try to build WebKit with VS2012. That cost me a lot of
Recently, I had the need for a function that I could use to guarantee
Recently I am making a server-client program using multithread concept. For some reason, I
Recently, I've started experimenting with Mercurial, due to the fact that it always attracted
Recently in a technical interview, I was asked to write a program to find
Recently I've studied the backpropagation network and have done some manual exercise. After that,
Recently me and my team got a HG repository for versioning as this would

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.