CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2000
    Location
    Stuttgart, germany
    Posts
    3

    Center Dialogs in Screen

    How can I center a Dialog in the middle of the screen or its parent window and how do i get a frames parent window?

    Thanks...


  2. #2
    Join Date
    Mar 2000
    Location
    Dublin, Ireland
    Posts
    124

    Re: Center Dialogs in Screen

    Evert,

    First, you need the screen size.

    For Applets:
    in the init() method:
    OR
    For Applications:
    in the constructor:
    Toolkit toolkit = this.getToolkit();
    Dimension d = toolkit.getScreenSize();


    At this point d.width and d.height contain your screen width and height, respectively.
    You should save these two values to global variables, such as:
    Dimension screen_size;
    screen_size.width = d.width;
    screen_size.height = d.height;


    Second, when you show your dialog(or when you instanciate it), insert the following:
    MyDialog myDialog = new MyDialog("My Dialog");
    // give the dialog a width and height(200 by 100)
    // don't worry about the 0,0 location yet
    myDialog.setBounds(0,0,200,100);
    // now you simply need to find the half-way point
    // of the screen and substract half the width(or height)
    // of the dialog...
    int x = (screen_size.width/2)-100;
    int y = (screen_size.height/2)-50;
    // reset the dialog's screen position...
    myDialog.setLocation(x,y);


    And that should do it.
    The process is pretty much the same if you're using Swing.
    Hope this helps.

    Regards,
    dogBear




  3. #3
    Join Date
    Apr 2000
    Location
    Hyderabad, India
    Posts
    23

    Re: Center Dialogs in Screen

    hai,

    U Can use

    public Dimension size=java.awt.ToolKit.getScreenSize();

    size will return the dimention of the screen so from the given dimensions u can center the screen.

    If u want to place at the center of the parent window or if u want to get the parent window u can use

    Object b=this.getParent() will return the parent of the current Object.

    if the parent is frame
    consvert the object into frame using
    Frame f=(Frame)b; // object converted before

    and the parent obtained u can use the centering the screen.

    Or the other way pass the size in the constructor to its child class which being involked.

    All the best

    -bharath
















    S.V Bharath Reddy
    Software Engineer,
    Hyderabad.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured