Chap 2 Swing
Chap 2 Swing
JLabel,JTextField,JButton,
Prepared BY
Mrs.Suwarna
Thakre
SWING
ButtonGroup Encapsulates a
mutually exclusive set
of buttons.
text field).
JScrollPane Encapsulates a
scrollable window.
void add(comp)
Here, comp is the component to be
added to the content pane.
Icons and Labels
ImageIcon(String filename)
The first form uses the image in the file named
filename.
ImageIcon(URL url)
import java.awt.*;
JLabel jl = new
import javax.swing.*; JLabel("Butterfly",
/*<applet code="JLabelDemo1"
width=250 height=150>
ii,
</applet>*/ JLabel.CENTER);
public class JLabelDemo1
extends JApplet // Add label to the
{ content pane
public void init() {
// Get content pane contentPane.add(
Container contentPane = jl);
getContentPane();
// Create an icon }
ImageIcon ii = new
ImageIcon("butter.gif"); }
// Create a label
JTextField
The Swing text field is encapsulated
by the JTextComponent class, which
extends JComponent. It provides
functionality that is common to Swing
text components.One of its subclasses
is JTextField, which allows you to edit
one line of text.
Constructors of JTextField
Some of its constructors are shown here:
JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
Here, s is the string to be presented, and
cols is the number of columns in the text
field.
Program using JTextField
import java.awt.*;
import javax.swing.*;
c.setLayout(new
/* FlowLayout());
<applet
code="textfield_swing"
jt = new
width=300 height=200> JTextField(15);
</applet>*/
public class textfield_swing c.add(jt);//add
extends JApplet textfield in
{
JTextField jt; container
public void init() }
{
Container c = }
getContentPane();
c.setBackground(Color.green);
JButton
The JButton class provides the functionality of
a push button. JButton allows an icon,a string,
or both to be associated with the push button.
Swing buttons are subclasses of the
AbstractButton class, which extends
JComponent
AbstractButton is a superclass for push
buttons, check boxes, and radio buttons.
Constructors of JButton
Some of its constructors are shown
here:
JButton(Icon i):- It creates a button with
no text and icon.
JButton(String s):- It creates a button
with the specified text.
JButton(String s, Icon i):- It creates a
button with the specified icon object.
Methods of JButton
String getText( )
It is used to obtained text
associated
with button
void setText(String s)
It is used to set the text to button
Methods of JButton
void setIcon(Icon b) :It is used to
set the
specified Icon on the button.
Icon getIcon(): It is used to get
the Icon of the button.
Program Using JButton
class
import java.awt.*; JButton b2=new
import javax.swing.*; JButton("NO",i2);
public class jbutton1 extends co.add(b1);
JApplet co.add(b2);
{ }
public void init() }
{ /*<applet code=jbutton1
Container width=200 height=200>
co=getContentPane(); </applet>*/
co.setLayout(new
FlowLayout(FlowLayout.CENT
ER,20,20));
ImageIcon ii=new
ImageIcon("logo1.gif");
ImageIcon i2=new
ImageIcon("butter.gif");
Output
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. Some of its
constructors are shown here:
JCheckBox
JCheckBox(Icon i):Creates an
initially unselected checkbox with an
icon.
JCheckBox(Icon i, boolean state)
Creates a checkbox with an icon and
specifies whether or not it is initially
selected.
JCheckBox
JCheckBox(String s)
Creates an initially unselected
checkbox with text.
JCheckBox(String s, boolean
state):Creates a checkbox with the
text and specifies whether or not it
is initially selected.
JCheckBox
JCheckBox(String s, Icon i):Creates an
initially unselected checkbox with the
specified text and icon.
JCheckBox(String s, Icon i, boolean
state)
Creates a checkbox with text and icon, and
specifies whether or not it is initially
selected.
Program using JCheckBox
import java.awt.*;
import javax.swing.*; ImageIcon i1=new
import java.awt.event.*; ImageIcon("emoji4.gif");
/* ImageIcon i2=new
<applet code="jcheckbox" ImageIcon("emoji5.gif");
width=400 height=50>
ImageIcon i3=new
</applet>
ImageIcon("emoji3.gif");
*/
cb1 = new
public class jcheckbox extends
JApplet
JCheckBox("A",i1);
cb2 = new
{ JCheckBox("B",i2);
Container co; cb3 = new
JCheckBox cb1,cb2,cb3; JCheckBox("C",i3);
public void init() co.add(cb1);
{ co.add(cb2);
co = getContentPane();
co.add(cb3);
co.setLayout(new
FlowLayout());
Output
Change background color of
button
import java.awt.*;
}
import java.applet.*;
public class buttonco }
extends Applet
{
/*<applet
public void init() code=buttonco
{ width=200
setLayout(null);
Button b1=new
height=200>
Button("ok"); </applet>*/
b1.setForeground(Color.gree
n);
b1.setBackground(Color.red);
b1.setBounds(30,50,120,80);
add(b1);
JRadioButton
Radio buttons are supported by the
JRadioButton class
Some of its constructors are shown here:
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
Constructors of
JRadioButton
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean
state)
Here, i is the icon for the button. The text is
specified by s. If state is true, the button is
initially selected. Otherwise, it is not.
Program using JRadioButton
import java.awt.*;
import javax.swing.*; JRadioButton b1 = new
/* JRadioButton("A",i1);
<applet code="jradiobutton1" co.add(b1);
width=300 height=50> JRadioButton b2 = new
</applet> JRadioButton("B",i2);
*/ co.add(b2);
public class jradiobutton1 extends JRadioButton b3 = new
JApplet JRadioButton("C",i3);
{ co.add(b3);
public void init()
ButtonGroup bg = new
{ ButtonGroup();
Container co = getContentPane();
bg.add(b1);
co.setLayout(new FlowLayout());
bg.add(b2);
ImageIcon i1=new
ImageIcon("butter.gif"); bg.add(b3);
ImageIcon i2=new }
ImageIcon("emoji4.gif"); }
ImageIcon i3=new
ImageIcon("emoji5.gif");
Output
JCheckbox Program using
EventHandling
import java.awt.*; cb.addItemListener(this);
import javax.swing.*;
contentPane.add(cb);
import java.awt.event.*;
/*
<applet code="checkboxdemo" width=400 }
height=50>
public void
</applet>
itemStateChanged(ItemEvent ie)
*/
public class checkboxdemo extends JApplet {
implements ItemListener JCheckBox cb1 =
{ (JCheckBox)ie.getItem();
Container contentPane;
JTextField jtf;
JCheckBox cb; if(cb1.equals(cb))
public void init() {
{ contentPane.setBackground(Color
contentPane = getContentPane();
.red);
contentPane.setLayout(new FlowLayout());
}
cb = new JCheckBox("C"); }
}