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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:22:47+00:00 2026-06-19T01:22:47+00:00

I would like to create a program where the Jframe is able to move

  • 0

I would like to create a program where the Jframe is able to move freely on it’s own. Kind of like a translation / transition.

For example,

  1. Click on program to begin.

  2. Jframe spawns at location (0,0).

  3. Automatically move (animate) 100 pixels to the right so that the new coordinates are (100,0).

I know there’s the setLocation(x,y) method that sets the initial position once the program runs but is there a way to move the entire Jframe after the program starts?

  • 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-19T01:22:48+00:00Added an answer on June 19, 2026 at 1:22 am

    The basic concept would look something like this…

    public class MoveMe01 {
    
        public static void main(String[] args) {
            new MoveMe01();
        }
    
        public MoveMe01() {
    
            EventQueue.invokeLater(
                    new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    final JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new JLabel("Use the Force Luke"));
                    frame.pack();
                    frame.setLocation(0, 0);
                    frame.setVisible(true);
    
                    Timer timer = new Timer(40, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Point location = frame.getLocation();
                            Point to = new Point(location);
                            if (to.x < 100) {
                                to.x += 4;
                                if (to.x > 100) {
                                    to.x = 100;
                                }
                            }
                            if (to.y < 100) {
                                to.y += 4;
                                if (to.y > 100) {
                                    to.y = 100;
                                }
                            }
    
                            frame.setLocation(to);
    
                            if (to.equals(location)) {
                                ((Timer)e.getSource()).stop();
                            }
                        }
                    });
                    timer.setRepeats(true);
                    timer.setCoalesce(true);
                    timer.start();
    
                }
            });
        }
    }
    

    This is a pretty straight, linear animation. You’d be better of investigating one of the many animation engines available for Swing, this will provide you with the ability to change the speed of the animation based on the current frame (doing things like slow-in and slow-out for example)

    I would take a look at

    • Timing Framework
    • Trident
    • Universla Tween Engine

    Updated with a “variable time” solution

    This is basically an example of how you might do a variable time animation. That is, rather then having a fixed movement, you can adjust the time and allow the animation to calculate the movement requirements based on the run time of the animation…

    public class MoveMe01 {
    
        public static void main(String[] args) {
            new MoveMe01();
        }
    
        // How long the animation should run for in milliseconds
        private int runTime = 500;
        // The start time of the animation...
        private long startTime = -1;
    
        public MoveMe01() {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    final JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new JLabel("Use the Force Luke"));
                    frame.pack();
                    frame.setLocation(0, 0);
                    frame.setVisible(true);
    
                    Timer timer = new Timer(40, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
    
                            if (startTime < 0) {
                                // Start time of the animation...
                                startTime = System.currentTimeMillis();
                            }
                            // The current time
                            long now = System.currentTimeMillis();
                            // The difference in time
                            long dif = now - startTime;
                            // If we've moved beyond the run time, stop the animation
                            if (dif > runTime) {
                                dif = runTime;
                                ((Timer)e.getSource()).stop();
                            }
                            // The percentage of time we've been playing...
                            double progress = (double)dif / (double)runTime;
    
                            Point location = frame.getLocation();
                            Point to = new Point(location);
    
                            // Calculate the position as perctange over time...
                            to.x = (int)Math.round(100 * progress);
                            to.y = (int)Math.round(100 * progress);
                            // nb - if the start position wasn't 0x0, then you would need to
                            // add these to the x/y position above...
    
                            System.out.println(to);
    
                            frame.setLocation(to);
                        }
                    });
                    timer.setRepeats(true);
                    timer.setCoalesce(true);
                    timer.start();
    
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a musician and a programmer and would like to create my own program
I would like to create a program that will run in background as a
I would like to create a program in a linux/unix environment that runs from
I would like to create a console program in VB.net that would allow parameters.
I would like to create a C program that accepts an argument of the
I would like to create a C# program that creates an MSI based on
Hi I would like to create a small program that listens for copy comands
I would like to create a program that will enter a string into the
I would like to create a simple parallel Sieve of Erastosthenes Java program, that
I'm trying to create a message validation program and would like to create easily

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.