Chapter 2 - Swing
Chapter 2 - Swing
AWT components require java.awt package. Swing components require javax.swing package.
AWT components does not start with J letter. Eg Button Swing components starts with J letter eg JButton
Plug & Play is not supported in AWT. We can have different look and feel, plug & play component in
Swing.
Advanced Components are not available in AWT. Swing has many advanced features like JTabel, JTabbed pane
which is not available in AWT.
Using AWT, you have to implement a lot of things yourself. Swing has them many built in features.
Display of images with components is not possible We can display images with Components
in AWT
Prepared by Prof. Dr. S. H. Kulkarni
List of Swing Component
Jlabel
JButton, JToggleButton, JRadioButton
JTextField, JTextArea, JPasswordField
JCheckBox
JList
JComboBox
JTable
JTabbedPane
Jtree
Setting of ToolTips
JProgressBar
Method Purpose
void setEditable(boolean) Sets or indicates whether the user can
boolean isEditable() edit the text in the text area.
(defined inJTextComponent)
import javax.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());
jtp.addTab("Flavors", new FlavorsPanel());
add(jtp);
}}
Prepared by Prof. Dr. S. H. Kulkarni
JTabbedPane(Continues…)
import javax.swing.*;
public class ToolTipExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
//Creating PasswordField and label
JPasswordField value = new JPasswordField();
value.setBounds(100,100,100,30);
value.setToolTipText("Enter your Password");
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
//Adding components to frame
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}