Reputation: 1647
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?
Upvotes: 3
Views: 11662
Reputation: 1220
You can just modify you existing JFrame constructor pasting in the following code:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - this.getWidth();
int y = 0;
this.setLocation(x, y);
this.setVisible(true);
i.e. so it would looks something like this:
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - this.getWidth();
int y = 0;
this.setLocation(x, y);
this.setVisible(true);
}
Upvotes: 0
Reputation: 209052
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();
}
});
}
}
Upvotes: 9