0

I am building a for me complex program and is far from done. But I ran into a problem.

In the program I want a jtextfield where the user can input a filepath eg. C:/Users/User1/Pictures/Screenshots/Skärmbild (1).png

and then press a button and the image is immediately loaded into a jlabel. Since the picture will be at a unknown size and must fit the label i must also be scaled to fit the label.

How can I do this and what is the best way of doing it? Thank in advance! :)

  • *"In the program I want a jtextfield where the user can input a filepath"* Be kind to the user, offer them a button that pops a `JFileChooser` Use a file filter that limits the view to image types the JRE understands/can load. *"Since the picture will be at a unknown size and must fit the label"* An alternative is to put the label in a scroll pane. But if you intend to go ahead with scaling the image, should the aspect ratio remain the same? Should the image actively resize when the user resizes the frame? – Andrew Thompson Jan 02 '17 at 22:13
  • The `ImageList` source in [this answer](http://stackoverflow.com/a/11027533/418556) both uses a file chooser to select images, and displays them (unscaled) in a scroll pane. It also provides a 'preview list' of scaled images on the left hand side of the GUI. Go through it and give the code a try. It might help you make some decisions, and provide some tips. – Andrew Thompson Jan 02 '17 at 22:17
  • Thanks for your help. I will be the only user of this program so therefore I don't have to be kind to the user :). Also it is very important that the image is scaled and preserve the aspect ratio and that the whole picture fit in the label. Thanks for your help! –  Jan 03 '17 at 09:15
  • .. What progress have you made based on `ImageList`? – Andrew Thompson Jan 03 '17 at 10:06

0 Answers0