CHAPTER-4
JAVA GUI PROGRAMMING REVISION TOUR - II [Swing Controls]
Brief Summary of the Chapter:
In this chapter we shall
be revising the JAVA GUI programming concepts using Swing API through NetBeans IDE.Java GUI
applications are created through RAD tools with Classes, Object and
methods etc.
methods etc.
Key Points:
· Swing API includes graphical components for
building GUIs.
· Swing components can be either container or
non container component. · Swing
provide seven different Layout manager.
· Frame is a top level window with a title and
a border, created through jFrame component of
Swing.
Swing.
· Common properties of buttons are:
background, border, font, foreground, enabled, Horizontal
Alignment, Vertical Alignment.
Alignment, Vertical Alignment.
· Label control displays text, that the user
can not changed directly. · Label
is created through jLabel class component.
· TextField is created through jTextField class component.
· TextField is created through jTextField class component.
· Password field takes input without showing it
on the screen, created through jPasswordField
class component.
class component.
· TextArea is multiline component to display or
enter text, created through jTextArea class
component.
component.
· Checkbox is a rectangular area that can be
chacked or unchecked created
class component.
class component.
SOLVED QUESTIONS
1.What does getPassword()
on a password field return?
(a) a string (b) an
integer (c) a character array. Ans: (c) a character array
2. Which of the following
component is the best suited to accept the country of the user?
A. List
B. Combo box C. Radio button D.
Check box
Ans: B. Combo box
3. What command do you need to write in
actionPerformed() event handler of a button, in order to make it exit button?
a. System.out.println();
b. System.exit(0); c.
System.out.print() Ans: b. System.exit(0);
4.What method would you use, in order to
simulate a button’s(namely Okbtn) click event, without any mouse activity from
user’s side?
a. Okbtn.setText()
b.Okbtn.getText() c. Okbtn.doClick()
Ans: Okbtn.doClick()
5. What would be the name of the event
handler method in the ListSelection listener interface for a list namely CheckList to
handle its item selections?
a.
CheckListValueChanged() b.
getSelectedValue() c. clearSelection() Ans: a. CheckListValueChanged()
6. Which control displays
text that the user cannot directly change or edit?
a.TextField b. Checkbox
c. Combobox d. Label
Ans: d.Label
7.Which control provides
basic text editing facility?
a.TextField b. Checkbox
c. Combobox d. Label Ans: a. TexfField
8. Occurrence of an
activity is called:
a.
Function b. Class c. Object
d. Event
Ans: d.Event.
Ans: d.Event.
9. Which property is used
to set the text of the Label?
a. font b.text c.name d.
icon
Ans: b.text
10. The object containing
the data to be exhibited by the combo box by which property.
a. editable b. model
c.selectedIndex d.selectedItem
Ans: b. model
11. What is GUI programming?
Ans: A GUI(Graphical User Interface) is an
interface that uses pictures and other graphic entities along with text, to
interact with user.
12. How is swing related
to GUI programming?
Ans: We can create a GUI application on Java
platform using Swing API (Appli Interface), which is part of Java Foundation
Classes(JFC).
13. What is an event?
What is event handler?
Ans: An event is
occurrence of some activities either initiated by user or by the system. In
order to react, you need to implement some Event handling system in your
Application. Three things are important in Even Handling-
Event Source: It is the GUI component that generates the
event, e.g. Button.
Event
Handler or Event Listener:
It is implemented as in the form of code. It receives and handles events through
Listener Interface.
Event
Object or Message:
It is created when event occurs. It contains all the information about the event which
includes Source of event and type of
event etc.
14. What is the default name of action event
handler of a button namely TestBtn?
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){ }.
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){ }.
15. What property would
you set to assign access key to a button?
Ans: mnemonic property is
used to assign access key or shortcut (Alt + Key).
16. Which method can programmatically
performs the click action of a push button?
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){ }.
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){ }.
17. Which property would you set the setting
the password character as ‘$’? Ans:echoChar
18. Which method returns the password
entered in a password field? Ans: char [] getPassword().
19. Which list property do you set for
specifying the items for the list. Ans: model
20. Which method would you use to determine
the index of selected item in a list? Ans: int getSelectedIndex().
21. Which method would you use to insert an
item at specified index, in the list? Ans:
void setSelectedIndex( int index).
22. How you can determine whether 5th item in
a list is selected or not? Ans: isSelectedIndex(4).
23. Which method you would use to insert
‘Hello’ at 10th position in the Text Area control. Ans:void insert(“Hello”,
9).
24. Which method you would like to use to
insert an Icon (picture) on a Push Button. Ans: void setIcon(Icon).
25. Which property would you like to set to
make a Combo box editable? Ans: editable.
26. What is Layout
Manager? Name the layout managers offered by NetBeans?
Ans: Layout managers enable you to control
the way in which visual components are arranged in GUI forms by determining the
size and position of components within containers.
There
are seven types of layout are available-
· Flow Layout
· Flow Layout
Grid Layout
Card Layout
Spring Layout
Border Layout
GridBag Layout
Box Layout
27. Name three commonly
used properties and methods of the following controls.
(a) text field (b) text
area (c) Check Box
Ans: (a) Properties: text, font, editable.
Methods: void setText(), String
getText(), void setEditable(boolean).
(b) Properties: enabled,
editable, wrapStyleWord Methods: setText(), getText(), isEditable()
(c) Properties:font, text,
selected . Methods: getText(), isEnabled(), isSelected().
28. What is dispose()
used for ?
Ans: dispose() is used for hide and dispose
of the frame when the user closes it. This removes the frame from the screen and
frees up any resources used by it.
29. What is the
difference between-
(a) Text field & Text
area
(b) Text field & password field
(c) Radio Button &
Check Box
Ans: (a) A text field’s text property can
hold single line of text unless it is an HTML text. While a text area’s text
can hold any number of lines of text depending upon its rows property.
(b) Though a text field and a password field can obtain a single
line of text from the user, yet these are different.
A password field displays the obtained text
in encrypted form on screen while text field displays the obtained text in
unencrypted form.
(c) Radio Button: A jRadioButton control belongs to
JRadioButton class of Swing controls. It is used to get choices from the user. It is grouped
control, so that only one can be selected at a time among them. Radio Button works
in group, so that they must be kept in a ButtonGroup container control like so that only one can be
selected at the same time.
Some features of
jRadioButton control are-
It can be used to input choices typed input
to the application. Only one Radio button can be selected at a time.
They must be kept in a
Button Group container control to form a group.
Check box: A jCheckBox control belongs to JCheckBox
class of Swing controls. It indicates whether a particular condition is on or
off. You can use Check boxes to give users true/false or yes/no options. Check Boxes may works
independently to each other, so that any number of check boxes can be
selected at the same time.
selected at the same time.
Some features of
jCheckBox control are-
It can be used to input
True/False or Yes/No typed input to the application. Multiple check boxes can
be selected at the same time.
30. What is the
significance of following properties of a text area ?
(a) lineWrap (b)
wrapStyleword
Ans: (a) Defines Wrapping
featureenable/disable (b) Determines where line wrapping occurs. If true, the
component attempts to wrap only at word boundaries. This property is ignored
unless linewrap is set
to true.
31. What is the
significance of a button group? How do you create a button group?
Ans:
We must add a ButtonGroup control to the frame to group the
check boxes by using Button Group property of the check box. By dragging
buttongroup control from palette window.
32. What do you
understand by focus?
Ans: A Focus is the ability to receive user
input/response through Mouse or Keyboard. When object or control has focus, it can
receive input from user.
An object or control can
receive focus only if its enabled and visible property are set to true.
Most of the controls provides FOCUS_GAINED()
and FOCUS_LOST() method in FocusEvent by the FocusListener. FOCUS_LOST() is
generally used for validation of data.
You can give focus to an object at run time by invoking the requestFocus() method in the code.
Ex. jTextBox2.requestFocus();
You can give focus to an object at run time by invoking the requestFocus() method in the code.
Ex. jTextBox2.requestFocus();
33. What is meant by
scope of a variable?
Ans: In Java, a variable
can be declared any where in the program but before using them.
The area of program within which a variable
is accessible, known as its scope. A variable can be accessed within the block
where it is declared.
No comments:
Post a Comment