kien vu
kien vu

Reputation: 75

How can I set jFrame Location at right of Screen?

this.setLocationRelativeTo(null);

jFrame will show in the center of Screen. But i don't known how to set jFrame at the right of Screen.

Upvotes: 2

Views: 8236

Answers (2)

Radiodef
Radiodef

Reputation: 37855

You can do this yourself by calculating the position based on the size of the screen and the frame.

static void setLocationToTopRight(JFrame frame) {
    GraphicsConfiguration config = frame.getGraphicsConfiguration();
    Rectangle bounds = config.getBounds();
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);

    int x = bounds.x + bounds.width - insets.right - frame.getWidth();
    int y = bounds.y + insets.top;
    frame.setLocation(x, y);
}

The screen insets include places where a window is not expected to occupy, like task bars and the Mac menu bar. It's possible there is an OS where you can place something on the right side of the screen which would interfere with the window placement if you didn't subtract the insets. (I think Ubuntu can do that, actually, but I don't remember whether a window is placed on top of or behind the menu.)


Here's a simple MCVE demonstrating all four edges. Pressing one of the four buttons anchors the JFrame to that edge.

package mcve;

import javax.swing.*;
import java.awt.*;

public class WindowPlacement {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Window Placement");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JButton top = new JButton("Top");
            JButton left = new JButton("Left");
            JButton bottom = new JButton("Bottom");
            JButton right = new JButton("Right");

            frame.add(top, BorderLayout.NORTH);
            frame.add(left, BorderLayout.WEST);
            frame.add(bottom, BorderLayout.SOUTH);
            frame.add(right, BorderLayout.EAST);

            top.addActionListener(e -> setLocationToTop(frame));
            left.addActionListener(e -> setLocationToLeft(frame));
            bottom.addActionListener(e -> setLocationToBottom(frame));
            right.addActionListener(e -> setLocationToRight(frame));

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

    // Also see:
    // https://docs.oracle.com/javase/9/docs/api/java/awt/GraphicsEnvironment.html#getMaximumWindowBounds--
    static Rectangle getMaxWindowBounds(JFrame frame) {
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Rectangle bounds = config.getBounds();
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= insets.left + insets.right;
        bounds.height -= insets.top + insets.bottom;
        return bounds;
    }

    static void setLocationToTop(JFrame frame) {
        frame.setLocation(frame.getX(), getMaxWindowBounds(frame).y);
    }

    static void setLocationToLeft(JFrame frame) {
        frame.setLocation(getMaxWindowBounds(frame).x, frame.getY());
    }

    static void setLocationToBottom(JFrame frame) {
        Rectangle bounds = getMaxWindowBounds(frame);
        frame.setLocation(frame.getX(), bounds.y + bounds.height - frame.getHeight());
    }

    static void setLocationToRight(JFrame frame) {
        Rectangle bounds = getMaxWindowBounds(frame);
        frame.setLocation(bounds.x + bounds.width - frame.getWidth(), frame.getY());
    }
}

Upvotes: 7

wutBruh
wutBruh

Reputation: 67

If the component is not null and is shown on the screen, then the window is located in such a way that the center of the window coincides with the center of the component.

In other words, you should try frame.setLocation() and then some values (x,y) and look what fits properly

Edit: value 0,0 will give rightcorner so in ur case : this.setLocation(0,0);

Upvotes: 0

Related Questions