Results 1 to 2 of 2
  1. #1
    tyty5949 is offline Member
    Join Date
    Jan 2012
    Location
    Texas
    Posts
    1
    Rep Power
    0

    Exclamation How do I Update JFrame Size

    Ok so lets say i have a simple JFrame and i do:

    public int framewidth = 720;
    public int frameheight = 480;

    JFrame frame = new JFrame();
    frame.setSize(framewidth, frameheight);

    And then on another page of code i change those int's:

    ClassName frameclass = new ClassName();
    frameclass.framewidth = 1920;
    frameclass.frameheight = 1080;

    After that i get stuck because the JFrame size dosn't refresh. It stays the original 720, 480. The second class is a action listener to a JButton which changes the variables. If it is possible to refresh it somehow i would really appriciate some help. I also have the same problems with JLables but that isnt such a big deal. If any one can help PLEASE!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I Update JFrame Size

    Your problem is common for those starting out in Java. When you use primitives to set some property, changing the primitives later has no effect on the properties. For instance, say a JFrame has internal variables that hold its size (or better its preferred size), and by calling frame.setSize(framewidth, frameheight) in the JFrame's internal code you would probably be setting those variables, something like:

    Java Code:
    public void setSize(int width, int height) {
       this.internalWidth = width;
       this.internalHeight = height;
    }
    So when you call this method, the values held by your ints are passed into this method and used to set the JFrame's internal variables.

    Now if later on in your program you change your framewidth and frameheight variables, this will have no effect on the JFrame's internal variables since the two sets of variables are not linked in any way. If you need to reset a JFrame's size, you'll have to call the setSize(...) method all over again in order to change the JFrame's internal properties.

    Having said that, it's usually not a good idea to set a component's size but rather to let the layout managers do this for you.

Similar Threads

  1. JFrame Size
    By newbie123 in forum AWT / Swing
    Replies: 7
    Last Post: 09-08-2011, 06:30 PM
  2. How do I update a JFrame?
    By soulmed in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 09:45 PM
  3. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 03:24 AM
  4. Image size in a JFrame
    By nickbeacroft in forum AWT / Swing
    Replies: 2
    Last Post: 06-26-2008, 05:08 PM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 04:10 PM

Tags for this Thread

Posting Permissions

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