Containers

Containers
Container is a component as it is a subclass of Component class. It can use all the methods of Component class and an extra added feature is components can be added to this component (container).
1. Any class subclass of a Container class is known as a container.
2. Only components can be added to container.
3. Every container comes with a default layout manager that can be changed explicitly with setLayout() method.
The AWT provides four container classes. They are class Window and its two subtypes -- class Frame and class Dialog -- as well as the Panel class. In addition to the containers provided by the AWT, the Applet class is a container -- it is a subtype of the Panel class and can therefore hold components.

1. Window: - A top-level display surface (a window). An instance of the Window class is not attached to nor embedded within another container. An instance of the Window class has no border and no title.
2. Frame: - A top-level display surface (a window) with a border and title. An instance of the Frame class may have a menu bar. It is otherwise very much like an instance of the Window class.
Example: -
import java.awt.*;
class Example3 extends java.applet.Applet
{
public void init()
{
add(new Button("One"));
add(new Button("Two"));
}
public Dimension preferredSize()
{
return new Dimension(200, 100);
}
public static void main(String [] args)
{
Frame f = new Frame("Example 3");
Example3 ex = new Example3();
ex.init();
f.add("Center", ex);
f.pack(); f.show();
}
}
Output: -
C:\java\awt>javac Example3.java
C:\java\awt>java Example3

3. Dialog: - A top-level display surface (a window) with a border and title. An instance of the Dialog class cannot exist without an associated instance of the Frame class.
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
class MyDialog extends JDialog {
public MyDialog(JFrame parent) {
super(parent, "My dialog", true);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Here is my dialog"));
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose(); // Closes the dialog
}
});
cp.add(ok);
setSize(150, 125);
}
}
class Dialogs extends JApplet {
private JButton b1 = new JButton("Dialog Box");
private MyDialog dlg = new MyDialog(null);
public void init() {
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dlg.show();
}
});
getContentPane().add(b1);
}
public static void main(String[] args) {
run(new Dialogs(), 125, 75);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
Output: -
C:\java\awt>javac Dialogs.java
C:\java\awt>java Dialogs

4. Panel: - A generic container for holding components. An instance of the Panel class provides a container to which to add components.
Example: -
import java.awt.*;
class Example4 extends java.applet.Applet
{
public void init()
{
Panel p;
setLayout(new BorderLayout());
p = new Panel();
p.add(new TextArea());
add("Center", p);
p = new Panel();
p.add(new Button("One")); p.add(new Button("Two"));
Choice c = new Choice();
c.addItem("one"); c.addItem("two"); c.addItem("three");
p.add(c);
add("South", p); }
public static void main(String [] args) { Frame f = new Frame("Example 4");
Example4 ex = new Example4();
ex.init();
f.add("Center", ex);
f.pack(); f.show();
}
}
Output: -
C:\java\awt>javac Example4.java
C:\java\awt>java Example4


Free Web Hosting