I search for example code to fit image on Jpanel and example for method getScaledInstance
Example 2
https://www.programcreek.com/java-ap...ScaledInstance
https://www.programcreek.com/java-ap...magePanel.java

But the example code dont have method main and method to load Image into Panel
I try to write method to load image into Panel and it show no result
/* Select FileSelectionMode In FileChooser */
package layout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileSystemView;
 
/** <p>Title: ImagePanel</p> <p>Description: A graphical component used to show
 * a given IconImage. This panel is designed to auto-resize the image basing on
 * width assigned by layout managers, maintaining aspect ratio. A maximum height
 * can be specified too. Internal caching is used as long as width is not
 * changed to avoid to recalculate images scaling continuously.</p>
 * @author Bertoli Marco Date: 1-lug-2005 Time: 15.55.21 */
public class ImagePanel extends JLabel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//protected ImageIcon image;
	protected int maxheight = Integer.MAX_VALUE;
	protected int currentWidth;
	/** Maximum number of cached elements */
	protected int maxCache = 256;
	protected HashMap< ImageIcon, ImageIcon > cache =
			new HashMap< ImageIcon, ImageIcon >();
	/** Construct a new ImagePanel */
	public ImagePanel( ImageIcon image ) {
		super();
		this.setHorizontalAlignment( SwingConstants.CENTER );
		this.addComponentListener( new ComponentAdapter() {
			@Override
			public void componentResized( ComponentEvent e ) {
				if ( image != null ) {
					ImagePanel.this.resizeImage( image );
				}
			}
		} );
 
		JFrame editorFrame = new JFrame( "Image Demo" );
		editorFrame.setDefaultCloseOperation(
				WindowConstants.EXIT_ON_CLOSE );
		JLabel jLabel = new JLabel();
		jLabel.setIcon( image );
		JPanel panel = new JPanel();
		panel.setLayout( new BorderLayout() );
		panel.add( jLabel, BorderLayout.CENTER );
		JScrollPane scrollPane = new JScrollPane();
		editorFrame.getContentPane().add( scrollPane,
				BorderLayout.CENTER );
		scrollPane.setViewportView( panel );
		editorFrame.pack();
	}
 
	static String FileChooser() {
		JFileChooser jfc = new JFileChooser(
				FileSystemView.getFileSystemView().getHomeDirectory() );
		String fileName01 = " ";
		int returnValue = jfc.showOpenDialog( null );
		// int returnValue = jfc.showSaveDialog(null);
		if ( returnValue == JFileChooser.APPROVE_OPTION ) {
			File selectedFile = jfc.getSelectedFile();
			fileName01 = selectedFile.getAbsolutePath();
		}
		return fileName01;
	}
 
	public static ImageIcon ImageICon01( String fileName ) {
		BufferedImage image = null;
		try {
			image = ImageIO.read( new File( fileName ) );
		}
		catch ( Exception e ) {
			e.printStackTrace();
			System.exit( 1 );
		}
		ImageIcon imageIcon = new ImageIcon( image );
		return imageIcon;
	}
	/** Sets image to be shown on this panel
	 * @param image image to be shown in IconImage format */
	//public void setImage( ImageIcon image ) {
	//	this.image = image;
	//	resizeImage();
	//}
	/** Sets maximum height allowed for this component
	 * @param height maximum height allowed */
	public void setMaximumHeight( int height,  ImageIcon image ) {
		this.maxheight = height;
		cache.clear();
		if ( image != null ) {
			resizeImage( image );
		}
	}
	/** Helper method used to resize input image and display it. If image was
	 * already resized to current size, take the copy from local cache. */
	protected void resizeImage( ImageIcon image ) {
		// Resets cache if width has changed
		if ( currentWidth != this.getWidth() ) {
			currentWidth = this.getWidth();
			cache.clear();
		}
		// If this component size is not already defined, sets image without
		// resizing
		Dimension d = this.getSize();
		if ( d.width <= 0 || d.height <= 0 ) {
			this.setIcon( image );
			return;
		}
		// If cache contains image of the correct size, returns it.
		if ( cache.containsKey( image ) ) {
			this.setIcon( cache.get( image ) );
			return;
		}
		// Calculates ratio factor
		Image tmp = image.getImage();
		float scale = (float) d.width / (float) image.getIconWidth();
		if ( scale * image.getIconHeight() > maxheight ) {
			scale = (float) maxheight / (float) image.getIconHeight();
		}
		// Resizes image
		tmp = tmp.getScaledInstance( (int) ( scale * image.getIconWidth() ),
				(int) ( scale * image.getIconHeight() ), Image.SCALE_SMOOTH );
		ImageIcon resized = new ImageIcon( tmp );
		if ( cache.size() > maxCache ) {
			cache.clear();
		}
		cache.put( image, resized );
		this.setIcon( resized );
	}
	public static void main( String[] args ) throws Exception {
		String fileName = FileChooser();
		ImageIcon image01 = new ImageIcon( );
		image01 =	ImageICon01( fileName );
		new ImagePanel( image01 );
	}
}


Note that my method load image into Panel work when it was not combined with the previous code
package layout;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
 
class ImageDemo {
	static String FileChoose() {
		JFileChooser jfc = new JFileChooser(
				FileSystemView.getFileSystemView().getHomeDirectory() );
		String fileName01 = " ";
		int returnValue = jfc.showOpenDialog( null );
		// int returnValue = jfc.showSaveDialog(null);
		if ( returnValue == JFileChooser.APPROVE_OPTION ) {
			File selectedFile = jfc.getSelectedFile();
			fileName01 = selectedFile.getAbsolutePath();
		}
		return fileName01;
	}
	public ImageDemo( ImageIcon imageIcon01 ) throws Exception {
		SwingUtilities.invokeLater( new Runnable() {
			public void run() {
				JFrame editorFrame = new JFrame( "Image Demo" );
				editorFrame.setDefaultCloseOperation(
						WindowConstants.EXIT_ON_CLOSE );
				JLabel jLabel = new JLabel();
				jLabel.setIcon( imageIcon01 );
				JPanel panel = new JPanel();
				panel.setLayout( new BorderLayout() );
				panel.add( jLabel, BorderLayout.CENTER );
				JScrollPane scrollPane = new JScrollPane();
				editorFrame.getContentPane().add( scrollPane,
						BorderLayout.CENTER );
				scrollPane.setViewportView( panel );
				editorFrame.pack();
				editorFrame.setLocationRelativeTo( null );
				editorFrame.setVisible( true );
			}
		} );
	}
 
	public static ImageIcon ImageICon01( String fileName ) {
		BufferedImage image = null;
		try {
			image = ImageIO.read( new File( fileName ) );
		}
		catch ( Exception e ) {
			e.printStackTrace();
			System.exit( 1 );
		}
		ImageIcon imageIcon = new ImageIcon( image );
		return imageIcon;
	}
	public static void main( String[] args ) throws Exception {
		String fileName = FileChoose();
		ImageIcon image01 = new ImageIcon( );
		image01 =	ImageICon01( fileName );
		new ImageDemo( image01 );
	}
}