Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JFrame Movement Listening

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default JFrame Movement Listening

    I am writing a program where I have a varying sized JFrame. If the content in the JFrame is larger than the screen size, I am providing scrolling.

    Now I have the situation where the user could have two (or more) screens with different resolutions. What I am wanting to accomplish is to write a listener that does the following:
    1) Notices when the JFrame has been moved
    2) When the moving stops, Determine which screen the JFrame is being displayed on
    3) Resize the JFrame appropriately for the screen's resolution

    I currently have a check in place for it displaying on the default screen when the JFrame opens. Here is the code I have for that in case anyone is interested:
    JFrame puzzleFrame = new JFrame("Hashi Puzzle");
     
    				Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
     
    				puzzleFrame.setMaximumSize(dim);
    				puzzleFrame.setSize((iconSize*puzzleSize)+50,(iconSize*puzzleSize)+50);
     
    				if(dim.getWidth()<=puzzleFrame.getWidth() && dim.getHeight()<=puzzleFrame.getHeight())
    				{
    					puzzleFrame.setMaximizedBounds(new Rectangle(dim));
    					puzzleFrame.setExtendedState(puzzleFrame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
    				}
    				else
    				{
    					if(dim.getWidth()<=puzzleFrame.getWidth())
    						puzzleFrame.setSize((int)dim.getWidth()-50,puzzleFrame.getHeight());
    					if(dim.getHeight()<=puzzleFrame.getHeight())
    						puzzleFrame.setSize(puzzleFrame.getWidth(),(int)dim.getHeight()-50);
    				}

    I'm wanting to do the same basic thing, but with a listener for when the JFrame moves instead of just when the JFrame opens. Any help is appreciated.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JFrame Movement Listening

    For what its worth, its been a while since I played with it but I recall the ComponentListener on a mac not functioning properly when the JFrame is moved (don't have my mac in front of me at the moment to verify and test so take this with a grain of salt - but the events were only fired after the mouse was released, not sequentially for each mouse moved event as one would expect and as it behaves on Windows). Just a note in case you need cross-platform capability, you may want to test to be sure it accomplishes what you need.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: JFrame Movement Listening

    I'm just seconding what copeg said. I've seen that same thing happen on a mac.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Listening to 2 Key Presses
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2011, 05:09 PM
  2. Listening to JMenuItem keyboard focus
    By nik_meback in forum AWT / Java Swing
    Replies: 0
    Last Post: December 31st, 2010, 03:02 AM
  3. Replies: 1
    Last Post: December 6th, 2010, 09:09 AM
  4. movement
    By mlan in forum Java Theory & Questions
    Replies: 4
    Last Post: February 15th, 2010, 09:57 PM
  5. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM