0

Trying to learn Java Swing and have a mostly conceptual question:

I understand it is recommended to not hard-code sizes of components and instead let the LayoutManagers take care of resizing (e.g. due to resizable frame or different resolutions on different screens) components appropriately.

This seems to work for the components but I'm puzzled when it comes to the "contents" of the components as these seem to not change their size according to the changes in their component.

Examples: For me

  1. the font size in a JTextArea does not seem to increase if the JTextArea does (as discussed in Maximize font size while keeping text within JTextField)

  2. the size of an ImageIcon on a JButton does not resize when the JButton does (similar to resizing a ImageIcon in a JButton)

Wouldn't it be natural to have the contents in these components resize with their component (as a standard option in Swing)?

Would your solution be getting the size of the component when painting it and then resizing its content?

I tried that for 2., extending JButton and overriding getIcon() with something like this (as suggested in the link I believe):

   @Override
   public Icon getIcon() {
       Icon icon=super.getIcon();
       if(icon instanceof ImageIcon)
       {
           Image image=((ImageIcon)icon).getImage();
           Image newimg = image.getScaledInstance(Math.max(this.getSize().width-10,1),Math.max(this.getSize().height-10,1), java.awt.Image.SCALE_SMOOTH);
           icon=new ImageIcon(newimg);
       }
       return icon;
   }

Regardless of the above conceptual question I'd also appreciate help regarding this concrete example. One problem with this implementation seems to be that the JButton's size can't decrease anymore once it contains the suitably sized icon.

Seems silly to do that for every possible type of component and content.

  • You're correct. The font size of a Swing component does not increase as the size of the component increases. Generally, if you expect images to expand, you create the larger image and shrink it down to fit the component. It's visually more appealing. An expanding image becomes blocky. Generally, when I design a Swing GUI, I don't expect it to expand. If I expect it to expand, like a word processor, then I'll design the GUI for expansion. – Gilbert Le Blanc Mar 17 '21 at 18:27
  • *Wouldn't it be natural to have the contents in these components resize with their component (as a standard option in Swing)?* - no. I don't want the Font size to increase/decrease as I resize the frame. My eyes would go batty with the constant changes. I don't want the component on a form to increase in size as I resize the frame. I want to see more of the components so I don't need a scrollbar. – camickr Mar 17 '21 at 20:50
  • Check out [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/). It is a custom Icon that does resize based on the size of the component. – camickr Mar 17 '21 at 20:52
  • You can build your own `LayoutManager` by implementing the `LayoutManager2` interface, and than resize the content every time the parent `Container` resized. – Programmer Mar 18 '21 at 06:33
  • Thank you for all these helpful comments! I tried StretchIcon and it seems to work well. When you don't expect the content to resize, does that mean that you choose a fixed content size which might e.g. take a considerably smaller/larger proportion of the frame on screens that differ from yours? – Utterlycomplete Mar 19 '21 at 17:48

0 Answers0