0

I've searched the internet and StackOverflow and found lots of ways to get the current screen size but
that screen size isn't the display size, is there a way to get the display size and not the screen size?

I'm currently using the following lines of code to get the screen size:

//size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.add(panel);

//get screensize
int height = (int) screenSize.getSize().getHeight();
int width = (int) screenSize.getSize().getWidth();
System.out.println("Current system width & height: " + screenSize.width + ", " + screenSize.height);
frame.setSize(width, height);

Does anyone know a way to get the display size? Or is it not really possible?

JensDW
  • 5
  • 3
  • 2
    Most people think of screen size as synonymous with display size. When you use these terms, what, exactly, do you mean? What's the difference to you? – MarsAtomic Oct 26 '20 at 16:12
  • 2
    Also, why do you care. What is the requirement that you are trying to solve. If you think you need to know the "display" size, then you are probably approaching the solution incorrectly. – camickr Oct 26 '20 at 16:47
  • 1
    The last line suggests you're trying to set your frame to fullscreen. If thats your intention, take a look at https://stackoverflow.com/questions/11570356/jframe-in-full-screen-java – MDK Oct 26 '20 at 17:11
  • I wanted to tell the difference but it just sounds ridiculous. I have created a jframe that is fullscreen and now I want to place a button on the top right so I need my screensize to determine how much I need to go to the right but when I use this method, it displays correct on my tv but not on my laptop screen. I know it sounds dumb, I’m still learning. – JensDW Oct 27 '20 at 06:15
  • 1
    That does not at all sound dumb. I do believe though this is a [XY-Problem](http://xyproblem.info/). In order to get a component to the top right corner of your frame you do not need to know the frames size, your layout manager will do it for you. You can e.g. add a [BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) to your frame, put all current components into center and then add another panel with a right-aligned [FlowLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html) in the north section. A button there will appear top right. – MDK Oct 27 '20 at 12:56

1 Answers1

1

I want to place a button on the top right

And that is exactly why I asked for your actual requirement, not your attempted solution. Your attempted solution is wrong.

  1. Don't use a null layout. Swing was designed to be used with layout managers. The default layout manager for a JFrame is the BorderLayout. So you can create a JPanel and use a FlowLayout with right alignment (read the API for the proper constructor. Then you add your button to the panel and the panel to the frame.

So to add a component to the top/right the basic logic would be:

JButton button = new JButton(...);
JPanel top = new JPanel( new FlowLayout(...) );
top.add(button);
frame.add(top, BorderLayout.PAGE_START);

Read the Swing tutorial on Layout Managers for more information and examples of using a layout manager.

  1. Don't use the setSize() method. Generally you would use the pack() method AFTER adding all components to the frame and the frame would size itself based on the preferred size of all components added to the frame.

However in this case you want the frame maximized to you would use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

AFTER adding all the components to the frame.

camickr
  • 321,443
  • 19
  • 166
  • 288