Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with getting JFrame bounds inside a timer in Netbeans

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 ?.

like image 421
Thusitha Avatar asked Aug 07 '11 15:08

Thusitha


2 Answers

Try

int width = Foo.this.getWidth();

where Foo subclasses JFrame.

like image 105
mre Avatar answered Nov 15 '22 05:11

mre


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.

like image 22
camickr Avatar answered Nov 15 '22 05:11

camickr