JApplet
and Swing
The Component Class
§ A component is an object having a graphical
representation that can displayed on the screen and interact with the user.
§ Eg. Buttons, checkboxes etc..
§ The visible UI controls that the user
interact with all of which have been added to a Container are components.
§ Any thing that is derived from the class
component can be component.
§ The component class is having number of
methods for event handling, managing graphics, painting, sizing and resizing
Container
class
§ Container is subclass of component.
§ Containers like, Frames, dialogues, windows
and Panels are contain components and are themselves components.
§ A
container is responsible for lying out any components that is contains.
§ It is possible through layout manager.
JComponent
§ The JComponent class is subclass of Container class.
§ The class contains large
number of subclasses like, JButton, Jlabel, JCheckBox, JRadioButton etc.
§ Creating windows in Swing
§ The top level windows in Swing
are JFrame, JApplet, JDialog and Jwindow classes.
§ This top level windows are
called root containers.
§ There are many panes in Root
container like, JRootPane, JMenuBar, JLayeredPane, Content pane and Glass Pane.
Glass Pane
This is hidden by default. It is transparent but the paintComponent
method can be used. This pane can be useful when we want to paint an area that
already contains one or more components.
The layered pane
This pane provides ability so the one component can appear on the top
of other. It support the position to its contents, which consist of the content
pane and the optional menu bar.
The content pane
The container of the root pane's
visible components, excluding the menu bar.
The optional menu bar
The home for the
root pane's container's menus. If the container has a menu bar, you generally
use the container's setJMenuBar method to put the menu bar in the
appropriate place.
The container model is implemented in JRootPane class.
§ The most common method used to
get the content pane is :
getContentPane()
that returns a container object.
§ User interface components like
button, checkbox etc. layout managers must be done on a content pane.
The components are added to the content pane using
add() method.
Steps to create
user interface window:
1. Create root container with
JFrame or JApplet or JWindow or JDialog
2. Get container with use of
getContentPane
3. Create component
4. Add components to container
using add().
JApplet
JApplet class hierarchy
Java.lang.object
java.awt.Components
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
§ Japplet is extended version of applet that
adds support for the JFC/Swing component architecture.
§ To create Japplet import
javax.swing.Japplet;
§ Japplet is swing class from which you can
instantiate an applet.
§ Japplet is container as well as component.
§ Component is the name of a class that
defines objects you can display on screen.
§ Container
can hold and display components.
§ Container is a kind of component.
§ Every container is a component but every
component is not a container only some are.
§ Every japplet is container and so it is
also component.
JApplet simple
program.
/* JApplet simple program */
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics.*;
/*<object code=japp1.class width =200 height=200></object>
*/
public class japp1 extends JApplet
{
public void paint(Graphics g)
{
g.drawString("First
Japplet prog",10,10);
}
}
/* JApplet program with Font class and JLabel*/
import javax.swing.*;
import java.awt.*;
/*<object code=japp2.class width=200
height=200></object> */
public class japp2 extends JApplet
{
Container
con= getContentPane();
JLabel lbl=
new JLabel("Navgujarat College");
JLabel lbl1=
new JLabel(" BCA");
Font a=new
Font("Arial",Font.BOLD,36);
public void
init()
{
con.setLayout(new FlowLayout());
//
lbl1.setFont(a);
con.add(lbl);
con.add(lbl1);
}
}
getContentPane()
§ In version prior to java 5 you could not
add component directly to JFrame or JApplet.
§ Instead if you wanted to add a Component to
one of these Containers, you had to call the get ContentPane() method.
§ The getContentPane() method assigns a
reference to a Container named con and the Container reference is used later
with the setLayout and add() methods.
§ JFrame, JApplet, JDialog and JWindow are
root containers.
§ These root containers have several Panes,
JRootPane, JMenuBar, JLayeredPane, ContentPane and Glasspane.
§ The root container classes have methods
that forward requests to the JRootPane.
§ The most frequently used method to get the
content pane is: getContentPane()
JFrame
JFrame class hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
Difference between Japplet and JFrame
§ JApplet is used for web based application
§ JFrame is used for application
§ JApplet does not use main method instead of
that it requires applet or browser.
§ JFrame use main method
§ JApplet can not access local resources
§ JFrame can access local resources
JFrame:
§ It is extended version of Frame from awt.
§ Jframe can place other objects within it
for display.
§ Constructors:
JFrame() : it creates new frame that is initially
invisible.
JFrame(String title) : It creates new frame that is
initially invisible with specified title.
Methods:
§ Void setTitle(String): sets jframe title
§ Void setSize(int,int): sets a jframe size
in pixels with the width and height as argument.
§ String getTitle: returns jframe title.
§ Void setBounds(int,int,int,int): sets the
location of frame with height and width.
§ Void setLayout(LayoutManager mgr): This
method set the layout manager for this Container.
§ Void setResizable(boolean)
§ Boolean isResizable()
§ Void setVisible(boolean)
§ int getX() Returns X component of window
location.
§ int getY() Returns Y component of window location.
§ void setLocation(int x, int y)
Move the frame window to a new location on the screen.
The top left corner of the window is specified by x and y.
setDefaultCloseOperation()
§ JFrame.EXIT_ON_CLOSE: exit the program when
jframe is closed
§ WindowConstants.DISPOSE_ON_CLOSE: close the
frame, dispose the object and keep application running
§ WindowConstants.HIDE_ON_CLOSE: close the
jframe and continous running
§ WindowConstants.DO_NOTHING_ON_CLOSE : keep
jframe and continous running application. Means close button is disable.
To create user interface
window following steps :
§ Create a root container using
JFrame or JApplet or JDialog or JWindow.
§ Get the container using
getContentPane method
§ Create component
§ Attach the component to a
container using add() method
Creating JFrame window
§ A JFrame window is standard
style window.
§ It is subclass of Frame class.
§ When JFrame is create its size
is (0,0) and is invisible.
§ A JFrame close button does not
close the window, when it is pressed. It just make window invisible.
Window events of JFrame
§ JFrame generates following
window events:
§ windowOpened
§ windowClosed
§ windowDeiconified
§ windowDeactivated
§ windowClosing
§ windowIconified
§ windowActivated
/*Simple program of JFrame:*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class framedemo1
{
public static void main(String
args[])
{
JFrame frm1 = new JFrame("Frame program");
frm1.setSize(400,400);
frm1.setVisible(true);
Container con=
frm1.getContentPane();
JLabel lbl1=new
JLabel("Frame Program");
con.setLayout(new FlowLayout());
con.add(lbl1);
frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/* Program to create multiple JFrames*/
import javax.swing.*;
class myframe extends JFrame
{
myframe( String s)
{
super(s);
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class framedemo3
{
public static void main(String
args[])
{
myframe f1= new
myframe("Myframe f1 ");
myframe f2= new
myframe("Myframe f2 " );
}
}
/* JFrame program with different close option*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class winclose extends WindowAdapter
{
public void
windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public class framedemo
{
public static void main(String
args[])
{
JFrame frm1 = new JFrame("Frame program");
winclose wc= new winclose();
frm1.addWindowListener(wc);
frm1.setSize(400,400);
frm1.setVisible(true);
}
}
JButton
JButton Class hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JButton
When a button is clicked ActionEvent is
created. In button mouse press and mouse release events can be processed
separately. ActionListener interface should implement.
Image
can be used as JButton.
Constructors
§ JButton()
Creates a button with no set text or icon.
§ JButton(Icon icon)
Creates a button with an icon.
§ JButton(String text)
Creates a button with text.
§ JButton(String text, Icon icon)
Creates a button with initial text and an icon.
Method
§ void
addActionListener(ActionListener al)
This method adds the specified action listener to
receive action from this button
§ String
getActionCommand()
This method returns the action command generated by
the button.
§ void
removeActionListener(ActionListener al)
This method remove action listener.
§ Void setText(String lbl)
Sets the buttons label to the specified string
§ Void getText()
Return the label of the button.
§ Icon getIcon()
Return the icon of button.
§ Void setIcon()
Sets the icon of the button.
Programs of Button
/* Demo of Jbutton with image and text */
import java.awt.*;
import javax. swing.*;
import javax. swing.JApplet.*;
import java.awt.event.*;
/*<applet code=jbuttondemo.java
height =200 width=200></applet>*/
public class jbuttondemo extends
JApplet implements ActionListener
{
JButton j1,j2;
JLabel l1;
public void init()
{
Container con= getContentPane();
con.setLayout(new FlowLayout());
ImageIcon i2= new ImageIcon("test.jpg");
j1= new JButton("Click me",i2);
j2= new JButton("Clear");
l1= new JLabel(" ");
j1. addActionListener(this);
j2.addActionListener(this);
j2.setEnabled(false);
con.add(j1);
con.add(j2);
con.add(l1);
}
public void actionPerformed(ActionEvent ae)
{
String s=
ae.getActionCommand();
if(s.equals("Click me"))
{
l1.setText("Click me button
Pressed");
j2.setEnabled(true);
}
else
{
l1.setText(" ");
j2.setEnabled(false);
}
}
}
/* Program of JButton on JFrame */
import java.awt.*;
import javax. swing.*;
import java.awt.event.*;
class jbuttondemo1 extends JFrame
implements ActionListener
{
JLabel
l1;
JButton j1,j2,j3;
Container con;
jbuttondemo1()
{
setSize(400,400);
setVisible(true);
con=getContentPane();
con.setLayout(new FlowLayout());
l1=
new JLabel(" ");
j1= new JButton("First");
j2= new JButton("Second");
j3= new JButton("Third");
con.add(j1);
con.add(j2);
con.add(j3);
con.add(l1);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
j1. addActionListener(this);
j2.addActionListener(this);
j3. addActionListener(this);
}
public static void main(String args[])
{
jbuttondemo1 j1=new jbuttondemo1();
}
public void actionPerformed (ActionEvent
ae)
{
String s= ae.getActionCommand();
if(s.equals("First"))
{
l1.setText("First");
}
else
if(s.equals("Second"))
{
l1.setText("Second");
}
else
if(s.equals("Third"))
{
l1.setText("Third");
}
else
{
l1.setText("
");
}
}
}
JLabel
Class hierarchy of
JLabel
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.Jlabel
Jlabel object is a component for placing text or an
icon or both.
Jlabel display single line of read only text. User can
not change the text displayed through Jlabel.
§ A display area for a short text string or
an image, or both.
§ A label does not react to input events. As
a result, it cannot get the keyboard focus.
§ A JLabel object can display either text, an
image, or both.
§ You can specify where in the label's
display area the label's contents are aligned by setting the vertical and horizontal
alignment.
§ JLabel() Creates a JLabel instance with no image
and with an empty string for the title.
§ JLabel(Icon image) Creates a JLabel instance with
the specified image.
§ JLabel(String text) Creates a JLabel instance
with the specified text.
§ JLabel(Icon image, int alignment) Creates a
JLabel instance with the specified image. In the alignment. The alignments are
Jlabel.CENTER, Jlabel.LEFT, Jlabel.RIGHT
Methods of JLabel
§ Icon getIcon() Returns the graphic image that the label displays
§ String
getText() Returns the
text string that the label displays.
§ Void setIcon(Icon icon) Defines the icon this component will display.
§ Void setText(String text) Defines the single line of text this
component will display.
§ Int getHorizontalAlignment()
§ Int getVerticalAlignment()
§ void setHorizontalAlignment()
§ void setVerticalAlignment()
§ void setFont(Font f)
/* JLabel Program, JLabel with image and text */
import java.awt.*;
import javax.swing.*;
import javax.swing.JApplet.*;
/* <applet code=JLabelDemo.java
width =200 height=200></applet> */
public class JLabelDemo extends
JApplet
{
public void init()
{
Container con= getContentPane();
con.setLayout(new FlowLayout());
JLabel j1= new JLabel("Label
Demo");
ImageIcon i2= new ImageIcon("test.jpg");
JLabel j2= new JLabel();
j2.setIcon(i2);
JLabel j3= new JLabel();
j3.setText(" Demo of
method");
con.add(j1);
con.add(j2);
con.add(j3);
}
}
JCheckBox
§ This is subclass of
JToggleButton class.
§ It can be either selected or
deselected.
§ When it is selected it display tick mark.
§ When it is selected it
generates ActionEvent, ChangeEvent and ItemEvent.
Class hierarchy of JCheckBox.
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JCheckBox
§ An implementation of a check box of an item
that can be selected or deselected, and which displays its state to the user.
By convention, any number of check boxes in a group can be selected.
Methods of JCheckBox
§ Void setText(string) : sets the text for
the JCheckBox
§ String getText() : returns the JCheckBox
text
§ Void setSelected(boolean): sets the state
of the JCheckBox to true for selected or false for unselected
§ Boolean is Selected(): gets the current
state of JCheckBox
§ void setIcon(Icon i)
§ Icon getIcon()
/* Simple
program of CheckBox demo */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class checkdemo extends JFrame implements ItemListener
{
Container con;
JLabel l1,l2;
JCheckBox jc1,jc2,jc3;
checkdemo()
{
super("CheckBox
Demo");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setcheckbox()
{
con=getContentPane();
con.setLayout(new
FlowLayout());
l1= new JLabel("Select any
one fruit");
jc1= new
JCheckBox("Banana");
jc2= new
JCheckBox("Mango");
jc3= new
JCheckBox("Orange");
l2= new JLabel(" ");
con.add(l1);
con.add(jc1);
jc1.addItemListener(this);
con.add(jc2);
jc2.addItemListener(this);
con.add(jc3);
jc3.addItemListener(this);
con.add(l2);
}
public static void main(String
args[])
{
checkdemo c1= new checkdemo();
c1.setcheckbox();
}
public void
itemStateChanged(ItemEvent e)
{
int select=e.getStateChange();
if(select==ItemEvent.SELECTED)
{
if(e.getSource()==jc1)
l2.setText(jc1.getText());
else if(e.getSource()==jc2)
l2.setText(jc2.getText());
else if(e.getSource()==jc3)
l2.setText(jc3.getText());
else
l2.setText(" ");
}
}
}
JRadioButton
§ Radio buttons are used to
represent a collection of mutually exclusive option.
§ The radio buttons are created
using JRadioButton class.
§ JRadioButton class is subclass
of JToggleButton class.
§ The ButtonGroup is created
using ButtonGroup class.
Class hierarchy Of
JRadioButton
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JRadioButton
Program of
JRadioButton
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class radiodemo1 extends JFrame
implements ItemListener
{
Container con;
JLabel l1,l2;
JRadioButton jr1,jr2,jr3;
ButtonGroup b = new ButtonGroup();
radiodemo1()
{
super("RadioButton Demo");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setradiobutton()
{
con=getContentPane();
con.setLayout(new FlowLayout());
l1= new JLabel("Select any one fruit");
jr1= new JRadioButton("Banana");
jr2= new JRadioButton("Mango");
jr3= new JRadioButton("Orange");
l2= new JLabel(" ");
con.add(l1);
b.add(jr1);
con.add(jr1);
jr1.addItemListener(this);
b.add(jr2);
jr2.addItemListener(this);
b.add(jr3);
jr3.addItemListener(this);
con.add(jr1);
con.add(jr2);
con.add(jr3);
con.add(l2);
}
public static void
main(String args[])
{
radiodemo1 obj1= new radiodemo1();
obj1.setradiobutton();
}
public void
itemStateChanged(ItemEvent e)
{
int
select=e.getStateChange();
if(select==ItemEvent.SELECTED)
{
if(e.getSource()==jr1)
l2.setText(jr1.getText());
else
if(e.getSource()==jr2)
l2.setText(jr2.getText());
else
if(e.getSource()==jr3)
l2.setText(jr3.getText());
else
l2.setText(" ");
} } }
JPanel
§ The simplest swing container is Jpanel.
§ It is a plain, borderless surface that can
hold light weight UI component
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
/*Program
of that demonstrate multiple layouts in single root pane with use of JPanel */
import java.awt.*;
import javax. swing.*;
import javax. swing.JApplet.*;
import java.awt.event.*;
/*<applet code=jfielddemo.java height =200 width
=200></applet>*/
public class jfielddemo extends JApplet implements ActionListener
{
JButton jb1,jb2,jb3,jb4;
JTextField f1,f2,f3;
JPanel j1,j2,j3,j4;
public void init()
{
j1=new JPanel(new
FlowLayout());
j2=new JPanel(new
FlowLayout());
j3=new JPanel(new
GridLayout(1,4,5,5));
j4=new JPanel(new
FlowLayout());
f1= new JTextField(10);
f2= new JTextField(10);
f3= new JTextField(10);
j1.add(f1);
j2.add(f2);
j4.add(f3);
jb1= new
JButton("+");
jb1.addActionListener(this);
jb2= new JButton("-");
jb2.addActionListener(this);
jb3= new
JButton("*");
jb3.addActionListener(this);
jb4= new
JButton("/");
jb4.addActionListener(this);
j3.add(jb1);
j3.add(jb2);
j3.add(jb3);
j3.add(jb4);
Container con= getContentPane();
con.setLayout(new
GridLayout(4,1,0,0));
con.add(j1);
con.add(j2);
con.add(j3);
con.add(j4);
f3.requestFocus();
}
public void actionPerformed(ActionEvent ae)
{
String s= ae.getActionCommand();
if(s.equals("+"))
{
int ans
=Integer.parseInt(f1.getText())+Integer.parseInt(f2.getText());
f3.setText(" "+ans);
}
else if(s.equals("-"))
{int ans
=Integer.parseInt(f1.getText())-Integer.parseInt(f2.getText());
f3.setText(" "+ans);
}
else if(s.equals("*"))
{int ans
=Integer.parseInt(f1.getText())*Integer.parseInt(f2.getText());
f3.setText(" "+ans);
}
else
{int ans
=Integer.parseInt(f1.getText())/Integer.parseInt(f2.getText());
f3.setText(" "+ans);
}
}
}