Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

AAYUSHI MAURYA-02

Advanced Java Programming (AJP)

Practical No. 8: Write a plogram to create a JTable.


X. Program Code-
1. Write a program code to generate the following output

Ans-

1|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. Name the superclass of JTable component.


ANS –
The JTable class is a part of Java Swing Package and is generally used to
display or edit two-dimensional data that is having both rows and columns.
Class Component (package java. awt ) is a superclass that declares the
common features of GUI components in packages java.
2. How rows are inserted in table.
ANS –

3. How to add JTable to JPanel ?


ANS –

To add JTabel to Panel, let us first crerate a panel −

JPanel panel = new JPanel();


Now, create JTable and add rows and columns with the records −

String[][] rec = {
{ "1", "Steve", "AUS" },
{ "2", "Virat", "IND" },
{ "3", "Kane", "NZ" },
{ "4", "David", "AUS" },
{ "5", "Ben", "ENG" },
{ "6", "Eion", "ENG" },
};
String[] header = { "Rank", "Player", "Country" };
JTable table = new JTable(rec, header);
Add the above created table to panel −

panel.add(new JScrollPane(table));

2|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Write a Java program to create a table of Name of Student, Percentage and


Grade of 10 students using JTable.
ANS –

3|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

4|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 9: Write a program to launch a JProgressBar


X. Program Code-
1. Write a program code to generate the following output

ANS-

5|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

6|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. Name different Orientation of Progress Bar.
ANS-
void setOrientation(int orientation)
It is used to set the orientation, it may be either vertical or horizontal by using
SwingConstants.VERTICAL and SwingConstants.HORIZONTAL
constants.

2. Explain the Purpose of setValue().


ANS-
The SetValue() method returns another ClassObject with the value as the
element passed as the parameter, and the key or label from the previous
ClassObject.

3. What is the use of minimum and maximum value of progressbar.


ANS-
 Maximum
It is used to set the maximum length of the progress bar control in the windows
form.
 Minimum
It is used to set or get the minimum value of the progress bar control in the
windows form.

7|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Develop a program to demonstrate the use of JProgressBar.


ANS-

8|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write a Program using JProgressBar to show the progress of Progressbar


when user clicks on JButton.
ANS-

9|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

10 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 10: Write a program to demonstrate status of key


on Applet window such as KeyPressed, KeyReleased, KeyUp,
KeyDown.
X. Program Code-
1. Write a program to generate KeyEvent when a key is pressed and display
“Key Pressed” message.
ANS-

11 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Develop a program which will implement special keys such as function keys
and arrow keys.
ANS-

12 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

13 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. Name any four Event Listener interfaces.


ANS-
1) Event Listener Interfaces.
2) The ActionListener Interface.
3) The ItemListener Interface.
4) The KeyListener Interface.
5) The MouseListener Interface
6) The MouseMotionListener Interface
2. State the situation when all three events of KeyListener interface are
generated?
ANS-
1) public abstract void keyPressed (KeyEvent e);
It is invoked when a key has been pressed.
2) public abstract void keyReleased (KeyEvent e);
It is invoked when a key has been released.
3) public abstract void keyTyped (KeyEvent e);
It is invoked when a key has been typed.

14 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Elaborate the terms Event, Source and Listener.


ANS-
 Event –
Change in the state of an object is known as event i.e. event describes the
change in state of source. Events are generated as result of user interaction
with the graphical user interface components.
 Source-
The source is an object on which event occurs. Source is responsible for
providing information of the occurred event to it's handler. Java provide as
with classes for source object.
 Listener-
It is also known as event handler. Listener is responsible for generating
response to an event. From java implementation point of view the listener is
also an object. Listener waits until it receives an event. Once the event is
received , the listener process the event an then returns

2. List various methods of ActionListener interface.


ANS-
The ActionListener interface is found in java. awt. event package. It has only
one method: actionPerformed().

void actionPerformed(ActionEvent e)
Invoked when an action occurs

15 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

3. Develop a program to accept two numbers and display product of two numbers
when user pressed “Multiply” button.
ANS-

16 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

17 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 11: Write a program to demonstrate various mouse


events using MouseListener and MouseMotion listener interface.
X. Program Code
1. Debug the following Program code and write the output.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <APPLET CODE ="MouseDemo2.java" WIDTH=300 HEIGHT=200>
</APPLET> */
public class MouseDemo2 extends Applet implements MouseListener
{
Label l;
public void init()
{
setLayout(null);
l=new Label("Hello Mouse");
l.setBounds(50,150,200,100);
add(l);
}
public void mousePressed(MouseEvent e)
{
l.setText("Mouse Pressed no. of clicks:" + e.getClickCount() + "at position" +
e.getX() + ","+ e.getY());
}
public void mouseReleased(MouseEvent e)
{
l.setText("Mouse Released; # of clicks:"+e.getClickCount());
}
public void mouseEntered(MouseEvent e)
{
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
l.setText("Mouse exited");
}
public void mouseClicked(MouseEvent e)
{
l.setText("mouse clicked(# of clicks:"+e.getClickCount());
18 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

}
}
ANS-

19 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. List various methods of MouseListener and MouseMotionListener


ANS-
MouseListener interface
 public abstract void mouseClicked(MouseEvent e);
 public abstract void mouseEntered(MouseEvent e);
 public abstract void mouseExited(MouseEvent e);
 public abstract void mousePressed(MouseEvent e);
 public abstract void mouseReleased(MouseEvent e);
MouseMotionListener
 public abstract void mouseDragged(MouseEvent e);
 public abstract void mouseMoved(MouseEvent e);

2. Do all components generate the MouseEvent


ANS-
'Yes' all components generate mouse event in java.

3. Write the steps to obtain the coordinates of MouseClick


ANS-
The coordinates of the mouse whenever a click takes place can be found by
detecting the click event with an event listener and finding the event’s x and y
position.

4. Write the steps to register for MosueEvents


ANS-
There are four steps:
1) Include the following import statement:
import java.awt.event.*;
2) Include Implements MouseListener :
public class w2 extends Applet implements MouseListener {
}
3) Register addMouseListener method in the applet.
this.addMouseListener (this)
4) Define the five MouseListener methods:
public void mouseClicked(MouseEvent event){
}
public void mouseEntered(MouseEvent event){

20 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

}
public void mouseexited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}

21 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Write a program to change the background color of Applet when


user performs events using Mouse
ANS-

22 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write a program to count the number of clicks performed by the user


in a Frame window
ANS-

23 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

3. Write a program to demonstrate the use of mouseDragged and mouseMoved


method of MouseMotionListener
ANS-

24 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 12: Write a program to demonstrate the use of


JTextField and JPasswordField using Listener Interface.
X. Program Code

1. Write a program using JPasswordField to set the password character as ‘#’


instead of ‘*’
ANS-

25 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. Write the use of setEchoChar() method with suitable example.


ANS-
setEchoChar(char c) : set the echo character for JPasswordField.
For example
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class login12 extends JApplet
{
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
Label l1=new Label("PASSWORD:");
JPasswordField t2=new JPasswordField(20);
t2.setEchoChar('@');
add(l1);add(t2);
}
}
/*
<applet code= "login12.java" height= 200 width = 200>
</applet>
*/

26 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write the advantages of using JPasswordField over JTextField


ANS-
The advantages of JPasswordField is a sub-class of JTextField, therefore it
acts like an ordinary text field however it hides the real characters typed, by
displaying a set of echo characters like dots or asterisks (*) for the purpose of
security. This is known as password masking. JTextField is an input
component allowing users to add some text. JPasswordField in Java is a
special type of text field that allows you to hide or change the character being
displayed to the user.

3. Which component can be used to accept the multiline input from user.
ANS-
The JTextArea class provides a component that displays multiple lines of
text and optionally allows the user to edit the text. If you need to obtain only
one line of input from the user, you should use a text field.

27 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Write a program using JPasswordField and JTextField to


demonstrate the use of user authentication
ANS-

28 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write a program using JTextField to perform the addition of two numbers.


ANS-

29 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

3. Write a program using JPasswordField to accept password from user and if


the length is less than 6 characters then error message should be displayed
“Password length must be >6 characters”
ANS-

30 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

31 | P a g e

You might also like