Results 1 to 11 of 11
  1. #1
    Thez is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default Listener for JFrame size change

    Is there a listener that I can use that will listen for any size changes in the window/JFrame?

    If the user resizes manually or the user maximizes/restores then I need to be able to get the new size, compare it to the old one and then move objects depending on the change.

    Also, is there a way to know if a JFrame is maximized or not?

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		JFrame j = new JFrame();
    		j.setVisible(true);
    		
    		j.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsListener(){
    
    			@Override
    			public void ancestorMoved(HierarchyEvent e) {
    				System.out.println(e);				
    			}
    			@Override
    			public void ancestorResized(HierarchyEvent e) {
    				System.out.println(e);
    				
    			}			
    		});
    	}
    }
    Please run the code, you can continue thereafter ..
    dont worry newbie, we got you covered.

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Please see javadocs for Frame.getExtendedState() for Maximized thing ..
    dont worry newbie, we got you covered.

  4. #4
    Thez is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    Thanks, I'll give that a try.

    Another thing, currently I'm doing a while loop to get an object to move say...100 pixels.
    However (logicly) it does it instantly since I didn't tell it to wait a few milliseconds between moving a pixel.
    I tried using the wait(); method but it gives the IllegalMonitorStateException.
    Any ideas?

  5. #5
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e1) {}
    dont worry newbie, we got you covered.

  6. #6
    Thez is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    I got an odd problem with that one.
    Instead of moving the image one pixel, wait & show the change, and then repeat it just seems to wait until the distance has been covered and then it shows the new position, still doing it instantly but with a delay before it (the amount of pixels * msec)

    Using a println() I was able to find out that it does do the sleep part correctly but it doesnt seem to update the image itself until it reaches its end.

  7. #7
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    If you need precision timing with your animation .. adopt double buffering and control the display with System time ..
    dont worry newbie, we got you covered.

  8. #8
    Thez is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    No I dont need it to be precise, I just need to be able to move a JPanel somewhat smoothly.

    The sleep loop works fine itslef but the JPanel isnt being moved while the loop is going, it only moves before and after the loop, it goes from x=0 to x=100 instead of x=1,x=2,x=3,x=4,x=5,x=6 and then moving.

    I tried panel.repaint(), frame.repaint(), validate() and a bunch of other stuff but it still doesnt work.
    I also tried creating a blank jFrame/class with just a JFrame and a JPanel to move but it had the same result.

  9. #9
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    dont worry newbie, we got you covered.

  10. #10
    Thez is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    I tried this.paint(getGraphics()); and that seems to have done it :D
    Last edited by Thez; 01-08-2008 at 09:56 PM.

  11. #11
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi
    I'm new to this forum donno abt asking questions
    i hav tried that coding for resizing program is getting executed but a lot of matter is being dispalyed in command prompt
    please help me

Similar Threads

  1. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 07:47 AM
  2. Tree with Listener not working when added to ScrollPane
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2008, 07:06 AM
  3. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  4. Listener collision on game
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:48 AM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •