Search code examples
javajframedesktop-application

aligning Jframe to different locations


i know that java being run on JVM makes it platform independent . i was recently learning to develop desktop apps so my question is: how to make my appscreen pop at the exactly top right/left or bottom right/left or centre? like, for top right we can simply use :
frame.setLocation(0,0)
or for the centre, one good , platform independent way i found here was:
frame.setLocationRelativeTo(null)

but what about the other 3 edges? i saw some big chunks of code using other classes (the dimensions/point class) or packages but either they were too complex or a perticular direction oriented(like one answer here i saw will easily make my app popup at the bottom; but if my teacher said he wants to see the app popup on top, i have to recode my entire solution).i also tried using getmaximumsize().height/width but to no good.

i wish if someone could assist me with a code handling all the possible edges and centre location(even if it uses other classes/packages;as long as it satisfies all the above cases,your answer is welcome). and another mini-question: are there any variables MAXWINDOW in java like , my window screen is 15.6" and the 0.5" toolbar below makes the ideal bottom for my 250*100 pixel jframe to be 0,15.5" , a variable telling that perticular ideal value? and changing its value according to location of my toolbar or operating systems having smaller/bigger sizes and toolbar locations??
it just came in mind because it felt like a variable like this could be quite handy and adds more to the platform-independency nature of java.

EDIT...
thanks to both @bjorn and @madprogrammer for their too good answers. i never thought that "getting taskbar-free window size for location" problem will need such a big code considering the platform independent clause. i feel bjorn's answer currently satisfies me completely. but mad's answer is more accurate(or precise?ugh my English) to the problems i quoted .


Solution

  • Toolkit.getDefaultToolkit().getScreenSize(); returns the full screen size of the default screen, it doesn't take into consideration other screen elements like the dock or taskbar, which aren't always at the bottom of the screen and aren't always the same size

    A better solution might be to make use of Toolkit.getDefaultToolkit().getScreenInsets and GraphicsConfiguration

    public static Rectangle getScreenViewableBounds(Window window) {
        return getScreenViewableBounds((Component) window);
    }
    
    public static Rectangle getScreenViewableBounds(Component comp) {
        return getScreenViewableBounds(getGraphicsDevice(comp));
    }
    
    public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
        Rectangle bounds = new Rectangle(0, 0, 0, 0);
        if (gd == null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            gd = ge.getDefaultScreenDevice();
        }
    
        if (gd != null) {
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            bounds = gc.getBounds();
    
            Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
            bounds.x += insets.left;
            bounds.y += insets.top;
            bounds.width -= (insets.left + insets.right);
            bounds.height -= (insets.top + insets.bottom);
        }
    
        return bounds;
    }
    

    This will return the "safe viewable" range of a specific screen. If you pass it null, it will use the "default" screen

    // Safe viewable area for default screen
    Rectangle bounds = getScreenViewableBounds(null);
    int x = bounds.x + ((bounds.width - getWidth());
    int y = bounds.y + ((bounds.width - getHeight());
    
    setLocation(x, y);
    

    Which would place the window in the bottom/right hand position of the window, BUT, place it aligned to the taskbar (because most people have it aligned along the bottom)

    For weird people, like me, who place the taskbar at the top of the screen

    // Safe viewable area for default screen
    Rectangle bounds = getScreenViewableBounds(null);
    int x = bounds.x;
    int y = bounds.y;
    
    setLocation(x, y);
    

    will place the window at the top/left corner of the viewable, aligned below the the taskbar

    And yes, I've seen too many developers use setLocation(0, 0) and place the window under the taskbar/menu bar, their names become "mud"