Ajp Notes Chap2
Ajp Notes Chap2
Swing( 10M)
The javax.swing package provides classes for java swing API such
as JButton, JTextField, JTextArea, JRadioButton, JCheckbox,
JMenu, JColorChooser etc.
First of all, by a heavy-weight, it means the code will take comparatively more time to
load and it will consume more System resources. AWT is considered to be heavy-
weight because its components are dependent on the underlying Operating System.
For instance, When we create an object of java.awt.Checkbox class, its underlying
Operating System will generate a checkbox for us. This is also the reason, AWT
components are platform dependent.
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which
simplify the development of desktop applications.
JApplet
Swing Components
1)JLABEL
import java.awt.*;
import javax.swing.*;
/*
<applet code="JLabelDemo" width=250 height=150>
</applet>
*/
public class JLabelDemo extends JApplet
{
public void init()
{
// Get content pane
Container contentPane = getContentPane(); //background to display
components
contentPane.setLayout(new FlowLayout());
// Create an icon
ImageIcon img = new ImageIcon("register.jpg");
// Create a label
JLabel j = new JLabel("HELLO", img,JLabel.LEFT);
// Add label to the content pane
contentPane.add(j);
}
}
2)JBUTTON
The JButton class provides the functionality of a push
button
Swing buttons are subclasses of the AbstractButton
class, which extends JComponent.
swingframe()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jb.setActionCommand("pressed");
jb.addActionListener(this);
contentPane.add(jb);
contentPane.add(t1);
}
public void actionPerformed(ActionEvent ae)
{
t1.setText(ae.getActionCommand());
}
}
3)JCHECKBOX
The JCheckBox class, which provides the functionality of a
check box, is a concrete implementation of
AbstractButton. Its immediate superclass is
JToggleButton, which provides support for two-state
buttons.
import java.awt.*;
import javax.swing.*;
/*
<applet code="JCheckBoxDemo" width=400 height=50>
</applet>
*/
public class JCheckBoxDemo extends JApplet
{
JTextField jtf;
public void init()
{
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Create icons
ImageIcon img1 = new ImageIcon("jpgIcon.jpg");
ImageIcon img2= new ImageIcon("Winter.jpg");
ImageIcon img3 = new ImageIcon("Sunset.jpg");
cb.setRolloverIcon(img2);
cb.setSelectedIcon(img3);
4)JRADIOBUTTON
Radio buttons are supported by the JRadioButton class,
which is a concrete implementation of AbstractButton. Its
immediate superclass is JToggleButton, which provides
support for two-state buttons
import java.awt.*;
import javax.swing.*;
/*
<applet code="JRadioButtonDemo" width=300 height=50>
</applet>
*/
public class JRadioButtonDemo extends JApplet implements
ActionListener
{
public void init()
{
JTextField tf;
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
tf = new JTextField(15);
contentPane.add(tf);
}
public void actionPerformed(ActionEvent ae)
{
tf.setText(ae.getActionCommand());
}
}
5)JCOMBOBOX
jc.addItemListener(this);
contentPane.add(jc);
// Create label
jl = new JLabel();
contentPane.add(jl);
}
public void itemStateChanged(ItemEvent ie)
{
String s = (String)ie.getItem();
ImageIcon i=new ImageIcon(s + ".jpg");
jl.setIcon(i);
}
}
6) JScrollPane
Scroll panes are implemented in Swing by the JScrollPane
class, which extends JComponent.
Constant Description
HORIZONTAL_SCROLLBAR_ALWAYS Always provide horizontal scroll bar
HORIZONTAL_SCROLLBAR_AS_NEEDED Provide horizontal scroll bar,if needed
VERTICAL_SCROLLBAR_ALWAYS Always provide vertical scroll bar
VERTICAL_SCROLLBAR_AS_NEEDED Provide vertical scroll bar,if needed
Here are the steps that you should follow to use a scroll pane in an
applet:
1. Create a panel object.
2. Create a JScrollPane object. (The arguments to the constructor
specify the component and the VALUES for vertical and horizontal
scroll bars.)
3. Add the scroll pane to the content pane of the applet.
import java.awt.*;
import javax.swing.*;
/*
<applet code="JScrollPaneDemo" width=300 height=250>
</applet>
*/
public class JScrollPaneDemo extends JApplet
{
public void init()
{
// Get content pane
Container contentPane = getContentPane();
int b = 0;
for(int i = 0; i <10; i++)
{
for(int j = 0; j <10; j++)
{
jp.add(new JButton("Button " + b));
b++;
}
}
// Add panel to a scroll pane
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
import javax.swing.*;
/*
<applet code="JTabbedPaneDemo" width=400 height=100>
</applet>
*/
getContentPane().add(jtp);
}
}
8)JTable
Tables are implemented by the JTable class, which extends
JComponent.
One of its constructors is shown here:
JTable(Object data[ ][ ], Object colHeads[ ])
import java.awt.*;
import javax.swing.*;
/*
<applet code="JTableDemo" width=400 height=200>
</applet>
*/
public class JTableDemo extends JApplet
{
public void init()
{
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
// Initialize data
Object data[][] = {
{ "abc", "CO5B", "8675" },
{ "xyz", "CO5B", "5555" },
{ "pqr", "CO5B", "5887" },
{ "lmn", "CO5B", "9222" }
};
}
}
9)JTREE
Trees are implemented in Swing by the JTree class, which
extends JComponent.
Some of its constructors are shown here:
JTree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
JTree(Vector v)
The getPathForLocation( ) method is used to translate a
mouse click on a specific point of the tree to a tree path.
The DefaultMutableTreeNode class implements the
MutableTreeNode interface.It represents a node in a tree
Here are the steps that you should follow to use a tree in an
applet
1. Create a JTree object.
2. Create a JScrollPane object. (The arguments to the
constructor specify the tree
and the policies for vertical and horizontal scroll bars.)
3. Add the tree to the scroll pane.
4. Add the scroll pane to the content pane of the applet.
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()
{
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JProgressBar
import javax.swing.*;
class ProgressBarExample extends JFrame
{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample()
{
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
}
Tool tip
You can create a tool tip for any JComponent with setToolTipText() method. This method is used to set
up a tool tip for the component.
For example, to add tool tip to PasswordField, you need to add only one line of code:
import javax.swing.*;