Saturday, February 15, 2014

How to auto adjust window size based on Image size in Java Swing


1. Put image as Icon / ImageIcon to the JLabel then
  • Test for MaximumSize for JFrame that returned Toolkit for concrete monitor
  • If PreferedSize is lower than MaximumSize size then call JFrame#pack()
  • otherwise have to call setSize()

2.Put image as Icon / ImageIcon by using Custom Painting to theJComponentJPanelJLabel e.i. then
  • then this JComponent must to returns PreferredSize
    a) call JFrame#pack() if PreferedSize is lower than MaximumSize,
    b) otherwise have to call JFrame#setSize()
    c) by assume that you don't use Image#getScalledInstance

3.Use Icon in the JLabel, there is only one issue that image can be smaller then expected size on the screen, but no issue with that, is pretty possible to centering image to the JLabel.CENTERto the JLabel

No comments:

Post a Comment