How to resize window keeping relative width and hieght

Ok as usual I do not want this done for me but I could use a pointer. Simply put I wish to set a JFrame so that when it is resized by the user it holds its relative width and height.
Could somebody point me to where it is at in the API or a generic sample of code that I can rework? I have read for it but am not finding it so far. Thank you for your patience and any help offered. If it matters I'm doing a fairly simple app in NetBeans not an applet or anything.
Edited by: Donalds on Apr 8, 2010 5:10 PM

This was the best I could think of off the top of my head.
You may want to request this thread be moved to the swing forums for better contributions.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class SampleFrame {
    public Dimension lastSize = new Dimension(400,300);
    public JFrame frame;
    public SampleFrame() {
        frame = new JFrame("Sample Frame");
        frame.setSize(lastSize);
        frame.setLocationRelativeTo(null);
        frame.addComponentListener(new ComponentListener() {
            public void componentResized(ComponentEvent e) {
                Dimension currentFrameSize = frame.getSize();
                int widthDelta = Math.abs(lastSize.width - currentFrameSize.width);
                int heightDelta = Math.abs(lastSize.height - currentFrameSize.height);
                if(heightDelta > widthDelta) {
                    double scaleRatio = currentFrameSize.getHeight() / lastSize.height;
                    currentFrameSize.width = (int) (lastSize.width * scaleRatio);
                } else {
                    double scaleRatio = currentFrameSize.getWidth() / lastSize.width;
                    currentFrameSize.height = (int) (lastSize.height * scaleRatio);
                frame.setSize(currentFrameSize);
                lastSize = currentFrameSize;
                frame.setLocationRelativeTo(null);
            public void componentMoved(ComponentEvent e) {
            public void componentShown(ComponentEvent e) {
            public void componentHidden(ComponentEvent e) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    public static void main(String[] args) {
        new SampleFrame();
}

Similar Messages

  • Bridge CC when copying resizes window to both monitors and enlarges all images to maximum size

    Bridge CC when copying an image resizes window to both monitors and enlarges all images to maximum size.

    Details, please.
    Please read this FAQ for advice on how to ask your questions correctly for quicker and better answers: 
    http://forums.adobe.com/thread/419981?tstart=0
    Thanks!

  • How can I get the image width and height stored in database?

    Hi!I write s servlet to display images store in database.but how can I get the image width and height?

    Have you tryed using PJA or a similar library?
    I presume you get java.lang.NoClassDefFoundError on the line :
    Toolkit.getDefaultToolkit();?

  • How to resize windows by pulling edges (like in Windows)

    Hi, is there a way to pull windows wider (resize them) by pulling on the window edges rather than the lower-right corner? I tend to have a lot of Terminal windows, as well as Photoshop windows.
      Windows XP Pro  

    Your profile says Windows XP pro, and doesn't say what Mac operating system you are running. Now most Mac operating systems do not let you pull the window edges to resize the windows. The only controls you'll get are in the corners.
    There have been some hacks on certain operating systems to make the edges work, but like I say they are hacks, and may not work through an operating system update. The last time I saw such a hack work was way back in System 7.6 with Church Windows. There may be additional ones on Mac OS X that work that have been mentioned on:
    http://www.resexcellence.com/
    Of course if you run Windows in emulation, or virtualization environment, Windows applications themselves will still have their usual interface behavior:
    http://www.macmaps.com/macosxnative.html#WINTEL
    tells of several such options.
    I'd add, I prefer the controls to remain on the corners, because otherwise you get confused how to move windows using their edges, or you might misdirect your cursor to the scrollbar and instead end up resizing the window when you don't want to.

  • How do i get the max width and height?

    Hey
    I want to get the max width and height, so my desktoppanes size can be set automatically.
    I know i should use getBounds but i cant find a concrete example on goole, on how to use it.
    Can someone point me in the right direction, with an example?

    Ok, assuming you want to get the screen resolution, i.e. 1024 x 768.
    You will need these imports:
    import java.awt.Dimension;
    import java.awt.Toolkit;and this line of code:
    Dimension myScreen =  Toolkit.getDefaultToolkit().getScreenSize() ;You can now use myScreen.width and myScreen.height to access the height and width of your screen's resolution. (width = 1024, height = 768 in my case)
    Hope this helps,
    Stern
    Edited by: Stern on Apr 13, 2008 6:49 AM

  • Relatives width and scrolling

    Hello,
    I just read manual for flex and have got small
    problem/question while playing.
    I am building application with one Panel and one button in
    vertical layout.
    I am setting Panel width to 100%.
    On start I see layout as expected: panel with width for
    entire page (excludding default padding) and button on next line.
    Now I am adding code to button - on click I am adding objects
    to panel, one by one in horizontal line.
    It is also ok, on button press you see that new objects are
    created.
    But, when entire panel is full of objects I expect it to show
    scroller for me, but instead of this panel increases in size
    itself, and this adds scroll to entire application, not panel.
    If I will set panel's width to absolute value (500) - it
    works ok, scroller is shown
    Could anybody explain, how can I make panel be 100% in width,
    and show scrolling for it content?
    my code is attached...
    thanks

    Unfortunately there is no option by which you can change the width of column in list.

  • How to automate the changing of Width and Height of the document

    Hi,
    I just want to know if anyone already automate the changing of width and height of document(eps file).
    Thanks,
    Buddy

    Check out SetDocumentSetup from kAIDocumentSuite.

  • How to resize window when stage style is undecorated ?

    Hi,
    I have one undecorated window and I want to make it resizable by using mouse.
    How can I do it ?
    If I use decorated window then it allows me to resize it.
    Thanks.

    Just make a draggable shape and resize the stage depending on the drag action.
    Here is a simple stage illustrating that and moving the window too.
    Note an old bug already seen in 1.1 at least: when dragging such window, on WinXP, sometime it seems to "jump" to one bound of the screen, then it goes back (in a flicker) to current position. Strange and annoying.
    def resizeHandle: Path = Path
        var origX = bind scene.width
        var origY = bind scene.height
        var startW: Number; var startH: Number;
        elements:
            MoveTo { x: bind origX, y: bind origY }
            LineTo { x: bind origX - 50, y: bind origY - 30 }
            LineTo { x: bind origX - 30, y: bind origY - 50 }
        fill: Color.CORAL
        stroke: null
        blocksMouse: true
        // Resize with the arrow
        onMousePressed: function (evt: MouseEvent): Void
            startW = stage.width;
            startH = stage.height;
        onMouseDragged: function (evt: MouseEvent): Void
            stage.width = startW + evt.dragX;
            stage.height = startH + evt.dragY;
    var keyHandler: Rectangle;
    def titleBar: Stack = Stack
        var title: Text;
        content:
            keyHandler = Rectangle
                width: bind title.layoutBounds.width * 1.2
                height: bind title.layoutBounds.height * 1.3
                fill: Color.web('#33FF55')
                blocksMouse: true
                // We can drag the stage by dragging the title bar
                onMouseDragged: function (evt: MouseEvent): Void
                    stage.x += evt.dragX;
                    stage.y += evt.dragY;
                onKeyReleased: function (evt: KeyEvent): Void
                    if (evt.code == KeyCode.VK_ESCAPE)
                        FX.exit();
                    println(evt);
            title = Text
                content: "Resizable and Draggable Window"
                font: Font { name: "Arial Bold", size: 24 }
                fill: Color.web('#331122')
    var scene: Scene;
    def stage: Stage = Stage
        title: "Undecorated Stage"
        style: StageStyle.UNDECORATED
        scene: scene = Scene
            width: 500
            height: 500
            fill: Color.GOLD
            content:
                Ellipse
                    centerX: bind scene.width / 2
                    centerY: bind scene.height / 2
                    radiusX: bind 0.45 * scene.width
                    radiusY: bind 0.45 * scene.height
                    fill: Color.AQUA
                Circle
                    centerX: bind scene.width - 55
                    centerY: bind scene.height - 55
                    radius: 50
                    fill: Color.PURPLE
                titleBar,
                resizeHandle
    keyHandler.requestFocus();

  • HT4818 how to resize windows partition

    How do you resize the windows partition in boot camp?

    iF you have already created the Windows partition, you can't resize it. To do it, you have to use an app like Paragon Camptune, that will allow you to resize your Windows volume > http://www.paragon-software.com/downloads/camptune.html
    The other option is to delete Windows and create the new partition with the size you want in Windows. Make a backup of your data before doing this

  • How to resize window (JFrame) on event

    I've got a swing application that uses tabs (JTabbedPane). It's default size is 200,200, and I need to resize the window to different sizes depending on what tab is selected. I can set the default size, but I can't seem to get access to the window to resize it (setBounds/setSize) at runtime.
    If anybody can tell me how I can go about this, I would greatly appreciate it.
    Matt
    Here's the code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class TimeTrack_Example extends JFrame {
         Container cp = getContentPane();
      public static void main(String args[]) {
              TimeTrack_Example TT = new TimeTrack_Example();
              TT.setSize(200,200);
      public TimeTrack_Example() {
              JTabbedPane JTPane = new JTabbedPane();
              JTPane.addChangeListener(cl);
                   //Jobs Tab
              JPanel jpJobs = new JPanel();
              JTPane.addTab("Jobs", jpJobs);
                   //Timer Tab
              JPanel jpTimer = new JPanel();
              JTPane.addTab("Timers", jpTimer);
                   //Report Tab
              JPanel jpReport = new JPanel();
              JTPane.addTab("Report", jpReport);
        cp.add(JTPane);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        pack();
        show();
              //Tab ChangeListener
         ChangeListener cl = new ChangeListener() { 
              public void stateChanged(ChangeEvent e) {
                   int tabID = ((JTabbedPane)e.getSource()).getSelectedIndex();
                   if (tabID == 2) {
                        //???               //here's where I need to change the size
    }

    This is one way:
    create a method, lets call it resizeWindow(). Call this method from inside your ChangeListener.
    public void resizeWindow() {
    this.setSize(300, 400); //whatever values you want
    public void stateChange(ChangeEvent e) {
    if (tabID == 2) {
    resizeWindow();
    }

  • How to resize window smoothly?

    I need to screen record a resizing a window but need it to happen in a very smooth manner.  I'd could manually drag the window but it won't be completely smooth or fluid.  I thought to use Automator but I don't see anything under resize. 
    I'd like to set starting and ending points.  If it could pause in the middle that would be great.
    Any ideas how this can be accomplished?

    Try the following:
    repeat 20 times
              set boundBox to bounds of window "Downloads" of application "Finder"
              set tFrame to (item 1 of boundBox)
              set lFrame to (item 2 of boundBox)
              set rFrame to (item 3 of boundBox) - 2
              set bFrame to (item 4 of boundBox)
              set bounds of window "Downloads" of application "Finder" to {tFrame, lFrame, rFrame, bFrame}
              delay 0.05
    end repeat
    In this script, change the "- 2" value to adjust the amount the window will change at each loop. The repetition count of 20 can be changed to also adjust the amount the window will change. Both of these will set the extent of the window change, and then you can adjust the delay value (in seconds) to change the rate once the other two values have been set.
    It will take some experimenting to figure

  • How to resize the Region's width

    Dear all,
    i am using apex 4.2.2 , oracle database 11g R2, internet explorer 9.
    i have created a region in HOME Tab for navigation menu.
    but its width is covering the width of whole page.
    how could i resize the width of the navigation region?
    Thank you so much.

    Hi Maahjoor ,
    you can set height with same code,
    just check with the different style properties
    in Region attributes put the below code.
    style="width:300px;height:200px;"
    Regards,
    Jitendra

  • Where is all Windows 7 related information, and what is my Product Key?

    Hi folks,
    I have a U150, I bought it from lenovo 15 days ago. And just now I realized that it has no windows related information in the package. In old times there was a sticker at the bottom of laptops that has product key, a booklet that has product key, Windows CD etc...
    The only windows related sticker is on the palmrest, right hand side of the touchpad and there is windows logo on it, nothing else.
    There was no CD, (this is ok of course hence no disk drive on U150), no booklet, no product key information, no piece of paper related to windows, just lenovo booklets..
    Are they missing? or this is the new trend? What if I want to re-install windows without using one-key recovery button? I think I can use a program that shows Win7 product key. Not only I do not trust them, (they can send this key somewhere next time computer connects to internet), but also I want to do this right way...
    Thanks in advance....
    Solved!
    Go to Solution.

    Oh,
    I just put the battery, plug it on the wall and started using it. Never tought it might be in there, after realizing looking for it. When I am home in the evening, I'll check..
    Thanks for the advice...
    Edit: Yes, it was there... thank you..

  • How do I return the page width and height as a text variable?

    Hi
    I'm trying to create a media box for use in job to return information in the slug area. I have a script that will return the username of the computer as a text variable but I would also like to return the height and width in mm as a text variable too. Does anyone know how to this please?
    Thanks

    Hi Vamitul
    Thank you for your response. I very new to scripting so I hope you'll bare with me.
    How do I apply your code to indesign please? At the moment I have a script that returns the computer user name(an online script i found called 'addCustomVariables_103.jsx') that's going to be one of the variables I use in my media box. This lives in the startup scripts folder but i'm guessing I can't just add your code to it as yours will require an open document to return any values. How would I apply your code so it works in inDesign? Ideally i could do with this automatically adding the text variables at startup as I'll be installing this on several machines.
    Thanks once again for your assistance.

  • When i try i download order and chaos for my 4s i click download but a download window keeps popping up and won't actually download?

    order and chaos will not download i just keep getting a download window but it wont download

    Hello deson12,
    It may help to force close the App Store, then restart your iPhone.
    iOS: Understanding multitasking
    http://support.apple.com/kb/HT4211
    iPhone, iPad, iPod touch: Turning off and on (restarting) and resetting
    http://support.apple.com/kb/HT1430
    Cheers,
    Allen

Maybe you are looking for

  • FireWire out to HD

    I'm editing on my PowerBook (soon on my MacBook Pro) and I will be needing a way to monitor out to HD. In SD, I just use a cheap little camera to act as a pass-thru to my Broadcast monitor. Now that I have the HVX200, it doesn't allow FireWire pass t

  • How can I operate Reader X in "Kiosk" mode?

    What I need to do: Open a single document via a command line reference and "lock" Reader from being able to go anywhere else. Initially opening the document is easy enough...just supply the complete pathname to the file on the command line.   I will

  • Using perl with Oracle DB

    Hi, I have perl scripts to import csv data to an existing 9.2 oracle database 32bit. It is using DBI to connect. I am moving the database in question to 10gR2 windows X64. I want the scripts to follow and run on the new server. I know there is an ins

  • Only Super Administration role can use collaboration rooms,

    The problem using portal 7.0 SP14, The users came from a SAP systems. the users with super administration role, Collaboration and CollaborationRoomCreator works perfectly, I mean, can create rooms and enter without problem, then i have an user that h

  • Scratch Disks etc in FCP 7

    Please can someone give me a simple answer to this question (last time I asked it, people answering got into an argument) Despite using FCP 7 very successfully so far, I'm still not quite sure that I have it set up to optimise stabilty and speed. Can