0

I have a Java Swing application with the following class:

public class ResolutionHandler {
  public static int calcSizeForResolution(int originalSize) {
    return (int) Math.round(originalSize * ((double) Toolkit.getDefaultToolkit().getScreenResolution()) / 96.0);
  }
}

Do I need to set each component's size via invoking this method, or would there be a simpler method to guarantee that when a component's size (be it height, width, row height, font size or any other dimension) is explicitly declared, the size value would be normalized according to the actual screen resolution?

András Hummer
  • 960
  • 1
  • 17
  • 35
  • 2
    Well, first thing we actually need to know that what do you know about `LayoutManager`. – Sage Dec 02 '13 at 14:16
  • What do you mean? What layout managers we usually use in our components or what other potential uses they may have I don't yet know about? – András Hummer Dec 02 '13 at 14:18
  • 1
    *"Do I need to set each component's size.."* You need to ***avoid*** that. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) Sage has pointed you in the right direction. Look into the layouts. – Andrew Thompson Dec 02 '13 at 14:20
  • 1
    After reading the above linked post start from: [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and after finishing learning some of them make sure to read: [Solving Common Layout Problems](http://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html) – Sage Dec 02 '13 at 14:23
  • So when it's about determining a size value, I should rather determine the size ratios of all components via their layout managers? – András Hummer Dec 02 '13 at 14:25
  • You should NOT determine the sizes. You let the layout manager to the job for you. Swing component have preferred sizes and you give hints to the component so it can calculate its own preferred size. For example with a text field you can specify the "columns". With a text area you can specify the "rows and columns". Read the tutorial and play with the examples. – camickr Dec 02 '13 at 16:39
  • In this case we have saved data plots on split panes with size and resizeweight stored in file. When we reopen those files, we need to restore the last state regardless of the rational looking preferred sizes. – András Hummer Dec 02 '13 at 16:50
  • Maybe this should help http://stackoverflow.com/questions/11256427/auto-adjusting-of-ui-based-on-screen-size-ui-in-java-swing – user3259321 Feb 01 '14 at 00:25

0 Answers0