Containers

As discussed earlier a container is a component that
can be nested. The most widely used Panel is the Class Panel which can be extended
further to partition GUIs. There is a Panel which is used for running the
programs. This Panel is known as Class Applet which is used for running the
programs within the Browser.
Common Container Methods
All the subclasses of the Container class inherit the behavior of more than 50
common methods of Container. These subclasses of the container mostly override
the method of component. Some of the methods of container which are most widely
used are as follow:
getComponents();
add();
getComponentCount();
getComponent(int);
ScrollPane
The ScrollPane container provides an automatic
scrolling of any larger component which was introduced with the 1.1 release of the Java Runtime Environment
(JRE). Any image which is bigger in size for the display area or a bunch of spreadsheet
cells is considered as a large object. Moreover there is no LayoutManager for a ScrollPane
because only a single object exists within it. However, the mechanism of
Event Handling is being managed for scrolling.
The example below shows the Scrollpane. This scrollpane
demonstrates the scrolling of the large image. In the program code below, first
of all we have created a scrollpane by creating its object, then we have passed
the parameter of image in it. We have also set the border layout as centre, as
shown.
import java.awt.*;
import java.applet.*;
class Scrollpane extends Component {
private Image image;
public Scrollpane(Image m) {
image = m;
}
public void paint(Graphics g) {
if (image != null)
g.drawImage(image, 0, 0, this);
}
}
public class ScrollingImageDemo extends Applet {
public void init() {
setLayout(new BorderLayout());
ScrollPane SC = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
Image mg = getImage(getCodeBase(), "cute-puppy.gif");
SC.add(new Scrollpane(mg));
add(SC, BorderLayout.CENTER);
}
}
|
|
Here is the output:
C:\newprgrm>javac ScrollingImageDemo.java
C:\newprgrm>appletviewer ScrollingImageDemo.html |

Download this example.

|