Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position on Screen Right Bottom

I need to position JFrame on my screen. But I can't make them appear on the right side of the screen bottom.

Please can someone explain me how to position them, if you can describe how to do it, it would be great.

Here is the code so far.

    //Gets the screen size and positions the frame left bottom of the screen
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int)rect.getMinX();
    int y = (int)rect.getMaxY()- frame.getHeight();
    frame.setLocation(x ,y - 45);
like image 789
Isuru Avatar asked Mar 17 '12 21:03

Isuru


People also ask

What is bottom right corner?

The bottom-right-corner page-margin box is a fixed-size box defined by the intersection of the bottom and right margins of the page box.

How do you set the bottom right button?

A simple problem but many new developers don't know how to do this. Set CSS Position property to fixed, bottom and right properties to 0px. Use cases : A feedback button or a chat window.

How do you keep a button in the bottom right corner?

If you want the button to be placed always at the bottom-right corner, use position:fixed instead of position:absolute .


2 Answers

Try the example below. Note how pack() "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."

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;

/** @see http://stackoverflow.com/q/9753722/230513 */
public class LowerRightFrame {

    private void display() {
        JFrame f = new JFrame("LowerRightFrame");
        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 = (int) rect.getMaxY() - f.getHeight();
        f.setLocation(x, y);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LowerRightFrame().display();
            }
        });
    }
}
like image 173
trashgod Avatar answered Sep 19 '22 23:09

trashgod


The easiest way I know is to nest JPanels each using its own layout manager.

  • The main JPanel would use a BorderLayout
  • Another JPanel that is added to the main at BorderLayout.SOUTH position also uses BorderLayout.
  • the component that needs to go at the SE corner is added to the above JPanel at the BorderLayout.EAST position.
  • In general, you're almost always better off using layout managers vs. trying to set absolute position of components.
like image 34
Hovercraft Full Of Eels Avatar answered Sep 19 '22 23:09

Hovercraft Full Of Eels