1

I am trying to move an label/icon & button from one place to another place using setLocation method but for some reason the objects are moving but not in motion. So if any one knows how to do this thing please tell me.

How to create a label or any component that moves from one place to another place in JFrame?

Below I have shown the code:

jl = JLabel
jf = JFrame

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==jbtn)
        {
            for(int i=0;i<=30;i++)
            {
                jl.setLocation(100,100+i);
                jl.repaint();
                jf.repaint();
                try
                {
                    Thread.sleep(50);
                }
                catch(Exception ae)
                {
                    ae.printStackTrace();
                }
            }
        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vighanesh Gursale
  • 921
  • 5
  • 15
  • 31
  • *"I am trying to move an label/icon & button from one place to another place"* Why? What feature does that provide to the user? What layout does the parent container use? – Andrew Thompson Nov 27 '12 at 08:07
  • i have kept the layout as null. I am trying to make an animation by moving the label. I am trying to make an animated login screen and for that purpose i want to know this thing. – Vighanesh Gursale Nov 27 '12 at 09:18
  • *"animated login screen"* Here's an idea. ***Don't*** animate a log-in screen. For log-ins and many other components, the users generally prefer the KISS principle of 'Keep It Simple, Stupid'. – Andrew Thompson Nov 27 '12 at 09:28

1 Answers1

4

Essentially you are blocking the thread (the Event Dispatching Thread) responsible for painting the updates

You might like to have a read of java timer change delay with button and Does displaying Java GUI requires some special treatment? which shows animation in swing and discusses the importance of the EDT.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366