2

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?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2582318
  • 1,607
  • 5
  • 29
  • 47

2 Answers2

8

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();
            }
        });
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • perfect i found this one here just now, i changed some "words" in my search and i got it.... i see the y = 0 ;) thank you so much ;* – user2582318 Jan 16 '14 at 18:22
0

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);
    }
CMP
  • 1,170
  • 12
  • 11