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. 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

import java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;

public
class ViewPortEg extends JFrame
{
      JViewport
jv;
      JPanel
pobj;
      JLabel
lblIcon;
      JScrollPane
js;
     
public ViewPortEg()
      {
            Icon ic=
new ImageIcon("./images/Lighthouse.jpg");
           
lblIcon=new JLabel(ic);
           
jv=new JViewport();
           
jv.add(lblIcon);
           
pobj=new JPanel();
           
pobj.add(jv);
           
js=new
JScrollPane(
pobj,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            getContentPane().add(
js);
            setSize(250,250);
            setVisible(
true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
     
public static void main(String[] args)
      {
           
new ViewPortEg();
      }

}


Example II
 

import java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;

public
class viewPortEg1 extends JFrame
{
      JList
list;
      JPanel
p1,p2,p3;
      JViewport
jv;
      JLabel
l1;
     
public viewPortEg1()
      {
           
p1=new JPanel();
           
list=new JList();
            String str[]={
"./images/Chrysanthemum.jpg","./images/deer.jpg","./images/Globe.jpg"};
           
list=new JList(str);
           
list.addMouseListener(new MouseAdapter()
            {
                 
public void mouseClicked(MouseEvent me)
                  {
                       
l1.setIcon(new ImageIcon(list.getSelectedValue().toString()));
                        }
                  });
           
p1.add(list);
           
l1=new JLabel(new ImageIcon(""));
           
p2=new JPanel()
           
jv=new JViewport();
           
jv.add(l1);
           
p2.add(jv);

           
p3=new JPanel();
           
p3.setLayout(new BorderLayout())

           
p3.add(p1,BorderLayout.WEST);
           
p3.add(p2,BorderLayout.EAST);

            getContentPane().add(
p3);
            setSize(250,250);
            setVisible(
true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }     

     
public static void main(String[] args) {
           
new viewPortEg1();\
     }
}


Example III

i
mport
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;

public
class ViewPortEg2 extends JFrame implements ActionListener

{
      JViewport
jv;
      JLabel
lblIcon;
      JPanel
p1,p2,p3;
      JButton
b1,b2,b3,b4;
     
public ViewPortEg2()
      {
           
p1=new JPanel();
            Icon ic=
new ImageIcon("./image/tiger-regal.jpg");

           
jv.add(lblIcon);
           
p1.add(jv);

           
p2=new JPanel();
           
b1=new JButton("Top");
           
b1.addActionListener(this);
           
b2=new JButton("Left");
           
b2.addActionListener(this);
           
b3=new JButton("Right");
           
b3.addActionListener(this);
           
b4=new JButton("Bottom");
           
b4.addActionListener(this);
           
p2.add(b1);
           
p2.add(b2);
           
p2.add(b3);
           
p2.add(b4);

           
p3=new JPanel();
           
p3.setLayout(new BorderLayout());
           
p3.add(p1,BorderLayout.CENTER);
           
p3.add(p2,BorderLayout.SOUTH);
            getContentPane().add(
p3);
            setSize(300,250);
            setVisible(
true)

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      public void actionPerformed(ActionEvent e)
      {
            JButton b=(JButton)e.getSource();


                  int x=(int)jv.getViewPosition().getX();                 
                 
int y=(int)jv.getViewPosition().getY()+1;
                 
jv.setViewPosition(new Point(x,y));}
           
else if(b==b2)
            {

                 
int x=(int)jv.getViewPosition().getX()+1;
                 
int y=(int)jv.getViewPosition().getY();
                 
jv.setViewPosition(new Point(x,y));                 
           
            else if(b==b3)
            {

                 
int x=(int)jv.getViewPosition().getX()-1;
                 
int y=(int)jv.getViewPosition().getY();
                 
jv.setViewPosition(new Point(x,y));           


           
else if(b==b4)
            {
                 
int x=(int)jv.getViewPosition().getX();

                 
int y=(int)jv.getViewPosition().getY()-1;jv.setViewPosition(new Point(x,y));               
            }

            public static void main(String[] args)
            {
            }
           
new ViewPortEg2();
      }
}

Hope you liked the example.

With Regards,
Vishal Gilbile.
 

Ebook Download
View all
Learn
View all