0

I'm working on Swing project, running on CentOS. I encountered a problem when invoke setLocation method of JFrame. My screen size is 1920 * 1080 and JFrame window size is 900 * 300. I want to the part of JFrame window out of the screen from right side, so I pass 1820 and 0 as the params into setLocation method, but the running result is the JFrame window not out of the screen, instead of the JFrame window right border align with the screen right border.
Does anyone knows what is wrong?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Gnayils
  • 161
  • 6
  • 1
    The window manager is capable of adjusting or ignoring the specified location, and some of them do that when the window would be (fully or partially) outside the screen. – kiheru Jan 07 '16 at 09:02
  • 1
    *"I want to the part of JFrame window out of the screen from right side"* Why would any **user** want that? – Andrew Thompson Jan 07 '16 at 09:37
  • @AndrewThompson Actually, i'm working on a onscreen virtual keyboard project, user will hide the virtual keyboard to outside of the screen when no long need to input. Imagine the behavior of the keyboard on Android or iOS. – Gnayils Jan 07 '16 at 13:57

1 Answers1

1

Your current approach has the effect of hiding the window's drag bar, which might preclude moving the window. The platform's window manager may forbid this. Instead, let the platform do the adjustment and then move the window relative to the GraphicsDevice bounds.

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @see http://stackoverflow.com/a/34651163/230513
 * @see http://stackoverflow.com/a/9755371/230513
 */
public class UpperRightFrame {

    private static final int VISIBLE = 100;

    private void display() {
        JFrame f = new JFrame("UpperRightFrame");
        f.add(new Box(BoxLayout.Y_AXIS) {
            {
                add(new JLabel(System.getProperty("os.name")));
                add(new JLabel(System.getProperty("os.version")));
            }

            @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);
        x = (int) rect.getMaxX() - VISIBLE;
        y = f.getLocation().y;
        f.setLocation(x, y);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new UpperRightFrame()::display);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you in advance but the code does not work in CentOS. I can see the code no problem in your OSX, actually, my code also no problem when running in Windows. – Gnayils Jan 07 '16 at 09:28
  • That's disappointing; I've added platform information to my [mcve], so that others can asses cross-platform behavior. – trashgod Jan 07 '16 at 10:52
  • For reference, Ubuntu 14 Unity also modifies the `setLocation()` request. – trashgod Jan 08 '16 at 10:36