How to position JFrame to top-right of screen upon start
10,657
As an example from this answer: positioning to the bottom-right, I made an adjustment to make it appear in the top-right
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TopRightFrame {
private void display() {
JFrame f = new JFrame("Top-Right Frame");
f.add(new JPanel() {
@Override // placeholder for actual content
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
});
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - f.getWidth();
int y = 0;
f.setLocation(x, y);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TopRightFrame().display();
}
});
}
}
Author by
user2582318
Updated on June 04, 2022Comments
-
user2582318 almost 2 years
how to put the JFrame on the TOP_RIGHT?
i know for center, and the normal one in the top left, but how to put in the TOP RIGHT?