Swing-related Classes
Dated: 04/23/2001
JApplet
Extends Applet.
Applets that use Swing must be subclasses of JApplet.
To add a compenent, do not invoke add() method of the Applet.
Instead call add() for content pane.
Container getContentPane ()
void add (Component comp) // add() of Container
Icons and Labels
Icons are encapsulated by the ImageIcon class, which paints an icon from an image.
Constructors:
ImageIcon (String filename)
ImageIcon (URL url)
Sample Code
import java.awt.*;
import javax.swing.*;
/*
<applet code="JLabelDemo" width=300 height=200>
</applet>
*/
public class JLabelDemo extends JApplet {
public void init () {
Container contentPane = getContentPane ();
ImageIcon ii = new ImageIcon ("image.gif");
JLabel jl = new JLabel ("My Image", ii, Jlabel.CENTER);
ContentPane.add (jl);
}
}
JButton
JButton(Icon i)
JButton(String s)
JButton(String s,Icon i)
void setActionCommand (String actionCommand)
Tabbed Panes
A tabbed Pane is a component that appears as a group of folders. Each folder has a title. When a user selects a folder, its contents become visible. Only one of the folders may be selected at a time. Tabbed panes are used for setting configuration options.
Tabbed panes are encapsulated by the JTabbedPane Class, which extends JComponent.
void addTab(String str, Component comp)
Typically a JPanel or a subclass of it is added.
The general procedure to use a tabbed pane:-
Sample Code
import java.swing.*;
/*
<applet code= "JTabbedPaneDemo" width=400 height=100>
</applet>
*/
public class JTabbedPaneDemo extends JApplet {
public void init() {
JTabbedPane jtp = new JTabbedPane();
jtp.addTab ("Cities", new CitiesPanel());
jtp.addTab ("Colors",new ColorsPanel());
getContentPane().add(jtp);
}
}
class CitiesPanel extends JPanel {
public CitiesPanel () {
JButton b1 = new JButton ("New York");
add(b1);
JButton b2 = new JButton ("London");
add(b2);
JButton b3 = new JButton ("Hong Kong");
add(b3);
}
}
class ColorsPanel extends JPanel {
public ColorsPanel () {
JButton jb1 = new JButton ("Red");
add(b1);
JButton jb2 = new JButton ("Green");
add(b2);
JButton jb3 = new JButton ("Blue");
add(b3);
}
}
Scroll Panes
Scroll Panes are implemented in swing by the JScrollPane class, which extends JComponents.
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int hsb)
vsb and hsb are int constants with follwing possible values:
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED
import java.awt.*;
import javax.swing.*;
/*
<applet code= "JScrollPaneDemo" width=400 height=100>
</applet>
*/
public class JScrollPaneDemo extends JApplet {
public void init () {
Container contentPane = getContentPane();
ContentPane.setLayout(new BorderLayout());
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(20,20));
int b = 0;
for (int i = 0;i < 20; i++) {
for (int j = 0;j < 20; j++) {
jp.add(new JButton("Button" + b));
++b;
}
}
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jp, v, h);
ContentPane.add(jsp,BorderLayout.CENTER);
}
}
Trees
A tree is a component that presents a hierarchical view of data.
Implemented in swing by the JTree class, which extends JComponent.
JTree (Hashtable ht)
JTree (Object obj[])
JTree (TreeNode tn)
JTree (Vector v)
The addTreeExpansionListener() and removeTreeExpansionListener() methods allow listeners to register and unregister for these notifications.
void addTreeExpansionListener(TreeExpansionListener tel)
void removeTreeExpansionListener(TreeExpansionListener tel)
TreePath getPathForLocation(int x, int y)
This method is used to translate a mouse click on a specific point of the tree to a tree path.
The TreeNode interface declares methods that obtain information about the tree node.
The MutableTreeNode interface extends TreeNode. It declares methods that can insert and remove child nodes or change the parent node.
The DefaultMutableTreeNode class implements the MutableTreeNode interface. It represents a node in a tree.
Sample Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/*
<applet code="JTreeEvents" width=400 height=200>
</applet>
*/
public class JTreeEvents extends JApplet {
JTree tree;
JTextField jtf;
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout ());
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
top.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
top.add(a2);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
top.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
top.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
top.add(b3);
tree = new JTree(top);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree,v, h);
contentPane.add(jsp,BorderLayout.CENTER);
jtf = new JTextField ("", 20);
contentPane.add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked (MouseEvent me) {
doMouseClicked(me);
}
});
}
void doMouseClicked (MouseEvent me) {
TreePath tp = tree.getPathForLocation (me.getX(), me.getY());
if (tp != null) {
jtf.setText(tp.toString ());
}
else
jtf.setText ("");
}
}