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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:22:01+00:00 2026-06-17T05:22:01+00:00

I just want to move a button when clicked on it and also I

  • 0

I just want to move a button when clicked on it and also I want to change the label text.

I wrote code for moving the button and it’s working fine for me. But when I want to change the label text to different name it’s not happening.

Either the button is moving from its place or the labels text is changed. But I want to perform both actions at once i.e. on button click event. I tried so many things as I could. Can some body help me?

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JavaGUI extends JPanel {

    private Control control = new Control();
    private Keys keys = new Keys("Original starting value.");

    public JavaGUI() {        
        this.add(keys);
        this.add(control);
    }

    private class Control extends JPanel {

        public Control() {
            this.add(new JButton(new AbstractAction("Update") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Command: " + e.getActionCommand());
                    keys.string = String.valueOf(System.nanoTime());
                    //keys.label.setText(keys.string);          
                    // If I remove these comments button will not move. bt I want both...
                    JButton j = (JButton) e.getSource();
            j.setLocation(j.getX()+10,j.getY()+10);
                }
            }));
        }
    }

    private class Keys extends JPanel {

        private String string;
        private JLabel label = new JLabel();

        public Keys(String s) {
            this.string = s;
            label.setText(s);
            this.add(label);
        }
    }

    private void display() {
        JFrame f = new JFrame("JavaGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JavaGUI().display();
            }
        });
    }
}
  • 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-17T05:22:02+00:00Added an answer on June 17, 2026 at 5:22 am

    You cannot move a button just like that, the layout manager tells it where it should be. Calling setText triggers the effect of layout manager for the whole component hierarchy. If you really want to do this, you should set a null layout manager, and manage all the sizes and locations of all components manually.

    Even if your button was moving in your original code, it was not painted correctly as soon as it left its original place – you could accidentally abuse Swing, but not completely.

    Working solution:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MovingButton {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    buildFrame();
                }
            });
        }
    
        private static void buildFrame() {
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            JPanel p = new JPanel();
            p.setPreferredSize(new Dimension(500, 500));
            p.setLayout(null);
    
            final JLabel label = new JLabel("Starting Text on Label");
            label.setLocation(200, 0);
            label.setSize(label.getPreferredSize());
            p.add(label);
    
            final JButton b = new JButton("Button");
            b.setSize(b.getPreferredSize());
            b.setLocation(0, 0);
            p.add(b);
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    b.setLocation((int)b.getLocation().getX() + 20, (int)b.getLocation().getY() + 20);
                    label.setText(String.valueOf(System.nanoTime()));
                }
            });
    
    
            f.add(p);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is for the move button. I want to move a selected list
I want to move a button to the center of the screen by code.
I want to move cells just as these two methods do: - (void)tableView:(UITableView *)tableView
I just want to add some extra text on top of input posted from
I have put a label into my frame, but it refuses to move. SetBounds()
I have a button on view1 and and I want to move it to
I just want to create an application with several functions. but i don't need
I'm trying to move button (with animation) upon click. I want it to move
I want to move div in and out on click of it. I just
Just want to ask if i can use Custom Validator in client side without

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.