I need to display an image in a JFrame. How can I make the window size to be automatically adjusted based on the size of the image.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1.in the case that you put image as
Icon / ImageIconto theJLabelthenhave to test for
MaximumSizeforJFramethat returned Toolkit for concrete monitorif
PreferedSizeis lower thanMaximumSizesize then callJFrame#pack()otherwise have to call
setSize()2.in the case that you put image as
Icon / ImageIconby usingCustom Paintingto theJComponent,JPanel,JLabele.i. thenthen this
JComponent must to returnsPreferredSizea) call
JFrame#pack()ifPreferedSizeis lower thanMaximumSize,b) otherwise have to call
JFrame#setSize()c) by assume that you don’t use
Image#getScalledInstance3.I’d be to use
Iconin theJLabel, 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 theJLabel.CENTERto theJLabel