Fetching screen resolution using Java

0 votes

Can someone guide me how can I fetch the screen resolution (width x height) in pixels using a JFrame and Java swing methods?

Sep 20, 2018 in Java by v.liyyah
• 1,300 points
4,799 views

1 answer to this question.

0 votes

You can fetch the screen resolution using the Toolkit.getScreenSize() method of Java. Try following the below code:

Dimension screenRes = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenRes.getWidth();
double height = screenRes.getHeight();

If you have a multi-monitor configuration, you need to use the following :

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

If you want to get the screen resolution in DPI you'll have to use the getScreenResolution() method on Toolkit.

For more info you can refer below:

Hope this helps!

Check out Java certification course to become certified expert.

Thanks!

answered Sep 20, 2018 by anto.trigg4
• 3,440 points

Related Questions In Java

0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,919 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,915 views
0 votes
1 answer

How can I read a large text file line by line using Java?

// Open the file FileInputStream file = new ...READ MORE

answered May 2, 2018 in Java by Parth
• 4,640 points
890 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,920 points
1,404 views
0 votes
1 answer

revalidate() vs repaint() in Java Swing

You need to call repaint() and revalidate() both in order ...READ MORE

answered Sep 20, 2018 in Java by code.reaper12
• 3,500 points
11,108 views
0 votes
1 answer

How to change selection listener in JComboBox?

It should respond to ActionListeners, like this: combo.addActionListener (new ...READ MORE

answered Nov 15, 2018 in Java by Frankie
• 9,830 points
1,160 views
0 votes
1 answer

Describe Heavy Weight Components Mean In Java Programming?

Heavy weight components like Abstract Window Toolkit ...READ MORE

answered Nov 28, 2018 in Java by Frankie
• 9,830 points
1,890 views
0 votes
1 answer

First panel added to a frame disappears

The default layout of a JFrame (or more specifically ...READ MORE

answered Mar 1, 2019 in Java by developer_1
• 3,350 points
1,449 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

answered Dec 21, 2019 in Java by Roberto
• 460 points

edited Jul 7, 2020 by Roberto 38,373 views
0 votes
3 answers

Setting time zone of a java.util.Date

Using Java 8 Java 8 introduced a new Date-Time ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,770 points
56,808 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP