Hi! I have made a desktop application which is running perfectly. But i am facing problems with jframe size on different machines.
I have monitor of 'Gateway'. I am satisfied with my jframe size on my screen . But when i shifted it on laptop of 'hp' company then my jframe and its child components appears very small.
For example I have a jframe named 'Home.java'. I set size of this jframe by following code

 java.awt.Dimension screenSize =                    Toolkit.getDefaultToolkit().getScreenSize();
 setSize(screenSize.width,screenSize.height);

On my monitor it appears very well i-e it takes monitor's screen size and flatters according that. But When i runs my program on 'hp' laptop it also flatters according to laptop's screen size but its child components(buttons and labels) appears very small.

Similarly I have a jframe named Sale.java. I have set its property resizeable(false).I do not need it to appear according to monitor's screen size. I have made this frame by giving a static size according to my desire.
On my pc when I runs program it appears very well i-e according to my given size and font size. But When I ran it on 'hp' laptop this jframe and its child components appear very small.

When I ran my program on 'Dell' laptop then my jframes and child components also appear with different size but not as small as on 'hp' laptop.

Is there any way that my jframes and their internal components appears in a good size according to machines.

Dani AI

Generated

, what you are seeing is normal when the UI is sized in raw pixels and then run on machines with different DPI/scaling. Instead of setSize(screen), let components declare their preferred sizes and let the window compute its size with pack(). That keeps your layout portable across screens and OS settings. (docs.oracle.com)
As noted, the key is using a real layout. For forms, GridBagLayout (or GroupLayout) lets fields grow, align, and reflow without hard‑coding sizes. Give components weights, fills, and insets rather than absolute dimensions. (docs.oracle.com)

Example pattern for your Home frame:

JFrame f = new JFrame("Home");
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.insets = new Insets(8, 8, 8, 8);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1.0;

p.add(new JLabel("Search"), gc);
gc.gridx = 1;
p.add(new JTextField(25), gc);

f.setContentPane(p);
f.pack();                     // sizes to preferreds
f.setLocationRelativeTo(null);
f.setVisible(true);

HiDPI tip: older JDKs rendered 1:1 pixels on Windows/Linux, which made Swing UIs tiny on dense laptop panels. If you can run on JDK 9+ you get built‑in HiDPI scaling and more consistent component sizing. (openjdk.org)
If you still need a nudge, scale Swing’s default fonts once at startup using the screen DPI (baseline 96):

int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
float scale = dpi / 96f;

UIDefaults d = UIManager.getLookAndFeelDefaults();
for (Enumeration<?> e = d.keys(); e.hasMoreElements();) {
    Object k = e.nextElement(), v = d.get(k);
    if (v instanceof FontUIResource f)
        d.put(k, new FontUIResource(f.deriveFont(f.getSize2D() * scale)));
}

This, plus responsive layouts as suggested, keeps your frames readable on Gateway, HP, and Dell alike. (docs.oracle.com)

Recommended Answers

All 2 Replies

That's what many call "responsive." It's a concept you implement in your code and not something that is say, one line long or covered in a small answer.

Example priors: https://www.google.com/search?q=responsive+java
Graphic examples: http://imgur.com/gallery/3PE7T "GIFs That Explain Responsive Design Brilliantl"
Source: http://blog.froont.com/9-basic-principles-of-responsive-web-design/

You might counter this is about web design but it applies to apps as well.

This is why Swing has Layout Managers. You start with the components (text fields, drop-down lists, whatever) which Java sizes to fit the text in them - nb this depends on the font you are using and the screen resolution. You use a layout manager to add these to the JFrame according to where you want them to go (top, bottom, etc). The Layout Manager then sets the size of the JFrame to fit your components according to the layout you wanted. This is done at run time so when you run on a different machine the frame is still sized appropriately. There is a decent choice of Layouit Managers, some simple, some less simple giving you detailed control over how things are spaced out, how they share the space when the frame is re-sized etc.
See https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.