problem with getting JFrame bounds inside a timer in Netbeans
Asked Answered
P

2

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

Prelude answered 7/8, 2011 at 15:7 Comment(1)
Try frame.getWidth() where frame is the JFrame you're referring to.Ishtar
M
5

Try

int width = Foo.this.getWidth();

where Foo subclasses JFrame.

Mahatma answered 7/8, 2011 at 15:12 Comment(4)
@Thusitha, Replace JFrame with the name of the class that subclasses JFrame.Mahatma
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).Storebought
@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.Mahatma
@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.Storebought
A
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.

Appreciable answered 7/8, 2011 at 15:14 Comment(1)
please can you explain it further...I'm quite a bit of new to java.Prelude

© 2022 - 2024 — McMap. All rights reserved.