5

I want to animate a JFrame to become half-size when i press a button in my programme. I think the easiest way is putting the current bounds of JFrame into a timer and decrease bounds 1 by 1 when the timer running.But when I declare a new timer in netbeans IDE it will looks like this.

      Timer t = new Timer(5,new ActionListener() {

        public void actionPerformed(ActionEvent e) {

          //inside this I want to get my Jframe's bounds like this
           //    int width = this.getWidth();---------here,"this" means the Jframe

           }

        }
    });

But the problem is in here "this" not refering to JFrame.And also I cant even create a new object of my JFrame.Because it will give me another window.Can anyone help me solve this problem ?.

mre
  • 43,520
  • 33
  • 120
  • 170
Thusitha
  • 3,393
  • 3
  • 21
  • 33

2 Answers2

5

Try

int width = Foo.this.getWidth();

where Foo subclasses JFrame.

mre
  • 43,520
  • 33
  • 120
  • 170
  • @Thusitha, Replace `JFrame` with the name of the class that subclasses `JFrame`. – mre Aug 07 '11 at 15:16
  • 2
    This should work if the timer code is internal to the main class, and if the main class subclasses JFrame (so 1+ upvote for this answer), but camickr's recommendation will work regardless of these restrictions (so 1+ to camickr's answer). – Hovercraft Full Of Eels Aug 07 '11 at 15:18
  • 1
    @Thusitha, The answer I gave was the "quick and dirty" approach. Here's another question that may illuminate why the answer I gave works - [What is the difference between Class.this and this in Java](http://stackoverflow.com/questions/5666134/what-is-the-difference-between-class-this-and-this-in-java). – mre Aug 07 '11 at 15:23
  • 1
    @Thusitha: again, I would advise you to go with camickr's recommendation. The above recommendation is good but will break if you refactor your code and put your control code (the listeners) in their own class separate from the view (the gui), and you will likely want to do this if this program grows. In programmer's parlance, I'd say that camickr's solution "scales" better. – Hovercraft Full Of Eels Aug 07 '11 at 15:38
5

I want to animate a JFrame to become half-size when i press a button in my programme

So when you click the button you have access to the button. Then you can use:

SwingUtilities.windowForComponent( theButton );

to get a reference to the frame.

So now when you create the ActionListener for the Timer you can pass in the Window as an argument for the ActionListener.

Edit:

The suggestion by mre is simple and straight forward and easy to use in many cases (and probably the better solution in this case).

My suggestion is a little more complicated but it was introducing you to the SwingUtilities method which will eventually allow you to write more reusable code that could potentially be used by any frame or dialog you might create.

A simple example would be something like:

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

public class AnimationSSCCE extends JPanel
{
    public AnimationSSCCE()
    {
        JButton button = new JButton("Start Animation");
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton button = (JButton)e.getSource();
                WindowAnimation wa = new WindowAnimation(
                    SwingUtilities.windowForComponent(button) );
            }
        });

        add( button );
    }


    class WindowAnimation implements ActionListener
    {
        private Window window;
        private Timer timer;

        public WindowAnimation(Window window)
        {
            this.window = window;
            timer = new Timer(20, this);
            timer.start();
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            window.setSize(window.getWidth() - 5, window.getHeight() - 5);
//          System.out.println( window.getBounds() );
        }
    }


    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("AnimationSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new AnimationSSCCE() );
        frame.setSize(500, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

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

Of course you would want to stop the timer when the winow reaches a certain minimum size. I'll leave that code up to you.

camickr
  • 321,443
  • 19
  • 166
  • 288