Java QnA Unit4
Java QnA Unit4
•
• A source generates an event in response to user action.
• Then source multicast the event to listeners.
• The listener receives the event and process it.
• An event is an object that describes a state change in a source.
• Example:-JCheckbox(event source) state change from ‘Unchecked’ to
‘Checked’ when used clicks on.
• Event is generated when a user interact with GUI element.
• Examples of user actions that generate events are: pressing buttons,
selecting an item in a list, clicking the mouse, pressing keys of a
keyboard, etc.
• An event source is an object that generates an event. (Eg:-JButton,
JCheckbox, JList, JChoice, JTextField, etc.)
• Sources may generate multiple events.
• A source must register listeners so that it can send notifications to
listeners about an event.
• The general form of registration method is:
• public void addTypeListener(TypeListener el)
• Where Type is the name of the event.
• The method that registers a mouse listener is called
addMouseListener().
• Event classes in java represent events.
• EventObject class is the superclass for all events.
• It is present in java.util package.
Q.3) What are java foundation classes? Explain important features of JFC.
They are a comprehensive set of GUI components and services which
dramatically simplify the development of desktop and web applications.
JFC consists of AWT, Swing, Java2D and few other classes.
The Swing components are the most notable part of JFC.
The full range of JFC features is delivered as part of JDK 1.2. Swing is a
toolkit that contains a new set of GUI components with a pluggable look
and feel.
Part of Java Foundation Classes.
JFC provides a variety of features:
1) Pluggable look and feel.
-Java Swing provides a pluggable look and feel architecture, which
allows developers to customize the appearance of their applications.
-Each platform has its own look and feel.
-This architecture allows Swing applications to look and feel like native
applications on any platform.
2)Layout Managers
-Swing adds more layout managers to those provided by the AWT.
-Layout managers save a lot of time and effort when developing
applications.
-The layout managers are used to set how components within a container
are positioned and sized.
3)Data Selection and Display Controls
-The controls like JList, JComboBox are data selection controls which
can handle large amounts of data easily.
-JTree is a very flexible control for displaying data organized in a
hierarchical form and JTable is used to display data that is organized in
two-dimensional row and column form and therefore is a natural choice
for representing data returned from queries made to a database.
•
import javax.swing.*;
import java.awt.*;
public class sdemo2 extends JFrame
{
JButton y, n, m;
public sdemo2()
{
setVisible(true);
setLayout(new FlowLayout());
y = new JButton("Yes");
n = new JButton("No");
m = new JButton("May Be");
add(y);
add(n);
add(m);
setSize(500,500);
}
public static void main(String args[])
{
sdemo2 s=new sdemo2();
}
}
import javax.swing.*;
import java.awt.event.*;
public class sdemo4 extends JFrame implements ActionListener
{
JLabel l,l1;
JRadioButton s1,s2,s3;
public sdemo4()
{
setLayout(null); setSize(600,600); setVisible(true);
l=new JLabel("Which of the following are your hobbies?");
l.setBounds(50,50,400,50);
s1=new JRadioButton("Playing Guitar");
s2=new JRadioButton("Listening Music");
s3=new JRadioButton("Watching Movies");
s1.setBounds(50,110,200,30);
s2.setBounds(50,150,200,30);
s3.setBounds(50,190,200,30);
ButtonGroup bg=new ButtonGroup();
bg.add(s1); bg.add(s2);bg.add(s3);
l1=new JLabel("");
l1.setBounds(50,250,300,200);
add(l); add(s1); add(s2); add(s3); add(l1);
s1.addActionListener(this);
s2.addActionListener(this);
s3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str="";
JRadioButton r=(JRadioButton)e.getSource();
if(r==s1)
str+=s1.getLabel()+" ";
if(r==s2)
str+=s2.getLabel()+" ";
if(r==s3)
str+=s3.getLabel()+" ";
l1.setText(str);
}
public static void main(String args[]) {
sdemo4 t=new sdemo4();
}
}
» JpasswordField:-
• JPasswordField allows the editing of a single line of text where the
view indicates something was typed, but does not show the original
characters.
• It is used as a password text field.
• It works exactly like JTextField, except that each character is shown
as a specified character, such as an asterisk.
• Important Methods:
• JPasswordField inherits from JTextField and hence it has all methods
of JTextField.
• It also has following methods defined in it:
• void setEchoChar(char c) //Sets the echo character for this
JPasswordField.
• char getEchoChar() // Returns the character to be used for
echoing.
» JTextArea:-
• Sometimes a single line of text input is not enough for a given task.
To handle these situations, the swing includes a simple multiline
editor called JTextArea.
• A JTextArea object is a multi-line region that displays text.
• It can be set to allow editing or to be read-only. [using setEditable()
method ]
• Following are the constructors for JTextArea:
• JTextArea( )
• JTextArea(int numLines, int numChars)
• JTextArea(String str)
• JTextArea(String str, int numLines, int numChars)
• BorderLayout:-
• A border layout lays out a container, arranging and resizing its
components to fit in five regions: north, south, east, west, and center.
• Each region may contain no more than one component, and is identified
by a corresponding constant: NORTH, SOUTH, EAST, WEST, and
CENTER.
• Constructors:-
• BorderLayout()-----Constructs a new border layout with no gaps
between components.
• BorderLayout(int hgap, int vgap)-----Constructs a border layout with
the specified gaps between components.
• At most five components can be added
• It is the default layout of a Frame.
• If you want more components, add a Panel, then add components to it.
• Following are the Fields present in BorderLayout for setting the region
where the Components are to be added.
• static String CENTER
• static String EAST
• static String WEST
• static String SOUTH
• static String NORTH
• When adding a component to a container with a border layout, use
one of these five constants, for example:
Panel p = new Panel();
Button b1=new Button("CLICK ME");
p.setLayout(new BorderLayout());
p.add(b1, BorderLayout.SOUTH);
b. Adapter classes:
• Adapter classes are abstract classes present in java.awt.event package.
• The adapter classes help programmers to do event handling with less
code.
• Every listener that includes more than one abstract method has got a
corresponding adapter class.
• The adapter classes are very special classes that are used to make event
handling very easy.
• There are listener interfaces that have many methods for event handling
and we know that by implementing an interface we have to implement
all the methods of that interface.
• But sometimes we need only one or some methods of the interface.
• In that case, Adapter classes are the best solution.
• Adapter classes contain default implementation for the corresponding
Listener interface.
• The following examples contain the following Adapter classes:
• ContainerAdapter class.
• KeyAdapter class.
• FocusAdapter class.
• WindowAdapter class.
• MouseAdapter class.
• ComponentAdapter class.
• MouseMotionAdapter class.
Q.15) Write a java swing program that display a label with an image.
import javax.swing.*;
public class sdemo1 extends JFrame {
public sdemo1()
{
setSize(400,500); setLayout(null);
setTitle("Label Example"); setVisible(true);
ImageIcon i=new ImageIcon("C:/Users/Agi/Downloads/ix.jpg");
JLabel l=new JLabel(i);
add(l);
l.setBounds(50,50,300,400);
}
public static void main(String[] args) {
sdemo1 f=new sdemo1();
}
}