JViewport class in java

JViewport class is a class which is similar to the control like that of picturebox in C# which is used for displaying images at runtime or at compile time.

JViewport class in java 

 
JViewport class is a class that is similar to the control like that of PictureBox in C# which is used for displaying images at runtime or at compile time. The "viewport" or "porthole" through which you see the underlying information. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom. JViewport class is there in javax.swing package.
 
Following are the examples of JViewport class:
 
Example I
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. import java.awt.event.*;  
  4. public class ViewPortEg extends JFrame {  
  5.  JViewport jv;  
  6.  JPanel pobj;  
  7.  JLabel lblIcon;  
  8.  JScrollPane js;  
  9.  public ViewPortEg() {  
  10.   Icon ic = new ImageIcon("./images/Lighthouse.jpg");  
  11.   lblIcon = new JLabel(ic);  
  12.   jv = new JViewport();  
  13.   jv.add(lblIcon);  
  14.   pobj = new JPanel();  
  15.   pobj.add(jv);  
  16.   js = new  
  17.   JScrollPane(pobj, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
  18.   getContentPane().add(js);  
  19.   setSize(250250);  
  20.   setVisible(true);  
  21.   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  22.  }  
  23.  public static void main(String[] args) {  
  24.   new ViewPortEg();  
  25.  }  
  26. }  
Example II
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. import java.awt.event.*;  
  4. public class viewPortEg1 extends JFrame {  
  5.  JList list;  
  6.  JPanel p1, p2, p3;  
  7.  JViewport jv;  
  8.  JLabel l1;  
  9.  public viewPortEg1() {  
  10.   p1 = new JPanel();  
  11.   list = new JList();  
  12.   String str[] = {  
  13.    "./images/Chrysanthemum.jpg",  
  14.    "./images/deer.jpg",  
  15.    "./images/Globe.jpg"  
  16.   };  
  17.   list = new JList(str);  
  18.   list.addMouseListener(new MouseAdapter() {  
  19.    public void mouseClicked(MouseEvent me) {  
  20.     l1.setIcon(new ImageIcon(list.getSelectedValue().toString()));  
  21.    }  
  22.   });  
  23.   p1.add(list);  
  24.   l1 = new JLabel(new ImageIcon(""));  
  25.   p2 = new JPanel()  
  26.   jv = new JViewport();  
  27.   jv.add(l1);  
  28.   p2.add(jv);  
  29.   p3 = new JPanel();  
  30.   p3.setLayout(new BorderLayout())  
  31.   p3.add(p1, BorderLayout.WEST);  
  32.   p3.add(p2, BorderLayout.EAST);  
  33.   getContentPane().add(p3);  
  34.   setSize(250250);  
  35.   setVisible(true);  
  36.   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  37.  }  
  38.  public static void main(String[] args) {  
  39.   new viewPortEg1();\  
  40.  }  
  41. }  
Example III
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. import java.awt.event.*;  
  4. public class ViewPortEg2 extends JFrame implements ActionListener {  
  5.  JViewport jv;  
  6.  JLabel lblIcon;  
  7.  JPanel p1, p2, p3;  
  8.  JButton b1, b2, b3, b4;  
  9.  public ViewPortEg2() {  
  10.    p1 = new JPanel();  
  11.    Icon ic = new ImageIcon("./image/tiger-regal.jpg");  
  12.    jv.add(lblIcon);  
  13.    p1.add(jv);  
  14.    p2 = new JPanel();  
  15.    b1 = new JButton("Top");  
  16.    b1.addActionListener(this);  
  17.    b2 = new JButton("Left");  
  18.    b2.addActionListener(this);  
  19.    b3 = new JButton("Right");  
  20.    b3.addActionListener(this);  
  21.    b4 = new JButton("Bottom");  
  22.    b4.addActionListener(this);  
  23.    p2.add(b1);  
  24.    p2.add(b2);  
  25.    p2.add(b3);  
  26.    p2.add(b4);  
  27.    p3 = new JPanel();  
  28.    p3.setLayout(new BorderLayout());  
  29.    p3.add(p1, BorderLayout.CENTER);  
  30.    p3.add(p2, BorderLayout.SOUTH);  
  31.    getContentPane().add(p3);  
  32.    setSize(300250);  
  33.    setVisible(true)  
  34.    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  35.    public void actionPerformed(ActionEvent e) {  
  36.     JButton b = (JButton) e.getSource();  
  37.     int x = (int) jv.getViewPosition().getX();  
  38.     int y = (int) jv.getViewPosition().getY() + 1;  
  39.     jv.setViewPosition(new Point(x, y));  
  40.    }  
  41.    else if (b == b2) {  
  42.     int x = (int) jv.getViewPosition().getX() + 1;  
  43.     int y = (int) jv.getViewPosition().getY();  
  44.     jv.setViewPosition(new Point(x, y));  
  45.     else if (b == b3) {  
  46.      int x = (int) jv.getViewPosition().getX() - 1;  
  47.      int y = (int) jv.getViewPosition().getY();  
  48.      jv.setViewPosition(new Point(x, y));  
  49.      else if (b == b4) {  
  50.       int x = (int) jv.getViewPosition().getX();  
  51.       int y = (int) jv.getViewPosition().getY() - 1;  
  52.       jv.setViewPosition(new Point(x, y));  
  53.      }  
  54.      public static void main(String[] args) {}  
  55.      new ViewPortEg2();  
  56.     }  
  57.    }  
I hope you liked the example.
 
With Regards,
 
Vishal Gilbile.