Java Swing How to - Add JLabel to JScrollPane, scroll and see the hidden part








Question

We would like to know how to add JLabel to JScrollPane, scroll and see the hidden part.

Answer

 // w  w  w.  ja va  2s.  c  o  m
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Main {
  public static void main(String[] args) {
    JLabel image = new JLabel("java2s.com");
    image.setPreferredSize(new Dimension(2000,2000));
    JScrollPane js = new JScrollPane(image);
    
    js.setPreferredSize(new Dimension(200,200));
    
    JOptionPane.showMessageDialog(null, js);
    
  }
}