2. NET BEANS IDE
It is used to create java applications using the efficient GUI builder.
IDE is an acronym for Integrated Development Environment which is a work
environment that integrates all tools necessary forApplication
Development and makes them available as part of one environment.
GUI is an acronym forGraphical User Interface which is an interface that
allows us to interact with the various components through visual elements
including pictures, graphical icons, symbols and visual indicators
3. COMPONENTS OF NET BEANS
COMPONENTS
TITLE BAR MENU BAR TOOL BAR GUI BUILDER PALATTE
INSPECTOR
WINDOW
PROPERTIES
WINDOW
CODE EDITOR
WINDOW
It is an area
to place
components
on the form
visually.
There are
two views of
the GUI
builder- the
DesignView
and the
SourceView.
It
contains
controls
or
compone
nts used
to create
GUI
applicatio
ns.
It is used to
display
hierarchical
relationship
among all
the
controls
placed on
the current
form.
It is used
to
view/edit
properties
of
currently
selected
control on
the form.
It is the
area
where
we write
code.
5. PROJECT, FORM AND
COMPONENTS
Each application is treated as
a Project in NetBeans.
Each project can have one or
multiple forms.
Each form can have one or
more components.
PROJECT
FORM1
COMPONENTS
FORM3
COMPONENTS
FORM4
COMPONENTS
FORM2
COMPONENTS
6. COMPONENTS/CONTROLS
CONTROLS
PARENT/CONTAINER
CONTROL
CHILD CONTROL
They act as background for other
controls. For eg. Frame. When we
delete/move a parent control, all its
child controls get deleted/moved.
Controls placed inside a
container control are called
child control. For eg. Button,
text field, label etc.
Some of these
components may be
visible and some
invisible.The visible
components are all
shown under the
FrameComponent
and the non-visible
components are part
of Other components
in the Inspectors
window.
7. DESIGNING AN APPLICATION
1. Create a new project.
2. Add a new jFrame form.
3. Add the components on this jFrame
.
4. Associate the code with the
component by double clicking the
component.
5. Add the source code.
6. Test the form by pressing shift+F6.
STEP 1
STEP 2
8. DESIGNING AN APPLICATION Contd…
OBJECTS
PROPERTIES METHODS
GETTERS SETTERS
EVENTS
Properties specify
the appearance of
an object on the
form. For eg. Font,
background etc.
These methods extract some
information from the object
and return it to the program.
They start with the word get.
For eg. getText(), isEditable()
etc.
These methods set some
properties of the object so
that the object's appearance
changes.They start with the
word set. For eg. setText(),
setForground(), etc.
These are the actions
performed on controls. For
eg.mouseClick, keyPressed
etc.When the user performs
any action on a control, an
event happens and that event
invokes the corresponding part
of the code and the application
behaves accordingly.
All the components including jFrame are
considered as objects in java.
Each object has some properties, methods,
and events associated with it.
9. CREATING A PROJECT
To create a new application project called “Students":
1. Choose File > New Project. Alternately, click the New
Project icon in the toolbar.
2. From the Categories pane select Java and in the Projects
pane, choose Java Application. Click Next.
3. Enter a name (in this case students) in the Project Name
field and specify the project location by clicking on the
Browse button. By default the project is saved in the
NetBeans Projects folder in My Documents.
4. Ensure that the Set as Main Project checkbox is selected
and clear the Create Main Class field.
5. Click Finish.
10. JFRAME FORM
Forms are used to accept data (input) from users and
respond to actions like clicking on a button.
ADDING FRAME:To create a JFrame Form container:
1. In the Projects window, right-click the Book node and
choose New > Jframe Form.
2. Enter Form Example 1 as the Class Name.This will be
the name of your form.
3. Enter Book as the package.This should be the name
given while creating the Project.
4. Click Finish
11. OPTION PANE
Option Pane is used when we want to request information from the user, display information
to the user or a combination of both. It requires the following import statement at the top of
the program.
import javax.swing.JOptionPane;
OR
import javax.swing.*;
METHOD DESCRIPTION EXAMPLE
showMessageDialog() Shows a one-button, modal dialog box that
gives the user some information.
JOptionPane.showMessageDialog(t
his,“Lets learn Java”);
showConfirmDialog() Shows a three-button modal dialog that asks
the user a question. User can respond by
pressing any of the suitable buttons.
Confirm=
JOptionPane.showConfirmDialog(nu
ll,"quit?")
showInputDialog() Shows a modal dialog that prompts the user for
input. It prompts the user with a text box in
which the user can enter the relevant input.
name=
JOptionPane.showInputDialog(this,“
ENTER ROLL NUMBER:");
12. Steps for developing a Simple application
Step 1: Create a new Project
Step 2: Add a JFrame form
Step 3: Add the desired component from the Palette window using drag
and drop feature
Step 4: Associate code with the component by double clicking the
component.
Step 5: Add the source code.
Step 6:Test the form by pressing Shift+F6.
DEVELOPING A SIMPLE APPLICATION
13. BUTTON CONTROL
A button is a component that the user presses or pushes to trigger a specific
action.When the user clicks on the button at runtime, the code associated
with the click action gets executed.
PROPERTY DESCRIPTION
BACKGROUND Sets background color
FOREGROUND Sets foreground color
FONT Sets font of text on button
TEXT Sets the text displayed on
button
METHODS DESCRIPTION EXAMPLE
setEnabled() Enables/disables the button JButton1.setEnabled(false); jbutton1.setEnabled(true);
setVisible() Visible/invisible the button JButton1.setVisible(false); jbutton1.setVisible(true);
15. LABEL CONTROL
Label provides text instructions or information. It displays a single line of
read-only text, an image or both text and image.
PROPERTY DESCRIPTION
BACKGROUND Sets background color
FOREGROUND Sets foreground color
FONT Sets font of text on button
TEXT Sets the text displayed on
button
METHODS DESCRIPTION EXAMPLE
isEnabled() Returns true if label is enabled else false Boolean b= jLabel1.isEnabled();
setVisible() Visible/invisible the button jLabel1.setVisible(false); jLabel1.setVisible(true);
setText() Sets the text on label at run time JLable1.setText(“Enter name”);
16. LABEL AND BUTTON CONTROL EXAMPLE
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.red);
}
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.green);
}
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.blue);
}
Q. WAP to create a form having a label and three buttons. If the red button is pressed the
text in the label should change to red colour and so on.
17. TEXT FIELD CONTROL
Text Field allows editing/displaying of a single line of text. For eg. Name, Phone
number etc.
PROPERTY DESCRIPTION
BACKGROUND Sets background color
FOREGROUND Sets foreground color
FONT Sets font of text on button
TEXT Sets the text displayed on button
METHODS DESCRIPTION EXAMPLE
isEnabled() Returns true if text field is enabled else false boolean b= jTextfield1.isEnabled();
setVisible() Visible/invisible the button jTextfield1.setVisible(false);
setText() Sets the text on text field at run time jTextfield1.setText(“WELCOME”);
getText() Gets the text from the text field string str = jTextfield1.getText();
isEditable() Returns true if the component is editable else false. boolean b= jTextfield1.isEditable();
setEditable() true if text in the text field is editable else false. JTextfield1.setEditable(true);
19. PASSWORD CONTROL
It is used to enter confidential input like passwords which are single line. It suppress the display
of input and each character entered can be replaced by an echo character. By default, the echo
character is the asterisk, *.
PROPERTY DESCRIPTION
BACKGROUND Sets background color
FOREGROUND Sets foreground color
FONT Sets font of text on button
TEXT Sets the text displayed on button
echoChar Sets the character that will be
displayed instead of text.
21. RADIO BUTTON CONTROL
Radio button is used when user has to select one option out of many mutually
exclusive options given. For eg. Gender(male or female),
PROPERTY DESCRIPTION
buttonGroup Specifies the name of the group of button to which
the jRadioButton belongs.
Selected Sets the button as selected, if set to true, default is
false.
METHODS DESCRIPTION EXAMPLE
isSelected() Returns true if radio button is checked else false boolean b= jRadiobutton1.isSelected();
setSelected() Checks(true) or unchecks the radio button. jRadiobutton1.setSelected(false);
setText() Sets the text on radio button at run time jRadiobutton1.setText(“WELCOME”);
23. TEXT AREA CONTROL
Text area allows editing/displaying of a multi line text. It automatically adds
vertical or horizontal scroll bars as and when required during run time. For eg.
Comments, address etc.
PROPERTY DESCRIPTION
lineWrap Indicates whether line of text should wrap in case it exceeds allocated
width.(Default is false)
wrapStyleWord Sends word to next line in case lineWrap is true else it results in
breaking of a word, when lines are wrapped.
rows/columns Sets number of rows/columns preferred for display.
text Sets the text displayed on button
METHODS DESCRIPTION EXAMPLE
isEnabled() Returns true if text field is enabled else false boolean b= jTextarea1.isEnabled();
setText() Sets the text on text field at run time jTextarea1.setText(“WELCOME”);
getText() Gets the text from the text field string str = jTextarea1.getText();
isEditable() Returns true if the component is editable else false. boolean b= jTextarea1.isEditable();
append() Adds text at the end JTextarea1.append(“we are studying JAVA”);
24. CHECK BOX CONTROL
Check box is used when multiple options are given to the user and the user
can select zero or more out of the given options.
PROPERTY DESCRIPTION
buttonGroup Specifies the name of the group of button to which
the check box belongs.
Selected Sets the check box as selected, if set to true, default
is false.
METHODS DESCRIPTION EXAMPLE
isSelected() Returns true if check box is checked else false boolean b= jCheckbox1.isSelected();
setSelected() Checks(true) or unchecks the check box. jCheckbox1.setSelected(false);
setText() Sets the text on check box at run time jCheckbox1.setText(“WELCOME”);
26. LIST CONTROL
A list is a scrollable set of items, used
to get one or more options out of
several given options which may or
may not be mutually exclusive.
Lists are preferred over checkboxes
when there are large number of
options.
In such case using Check Boxes may
take up a lot of space on the form and
it may also be inconvenient for the
user to select the desired options.
27. LIST CONTROL Contd…….
PROPERTY DESCRIPTION
model Contains the values to be displayed in the list.
selectedIndex Contains the index value of selected option of the control.
SelectionMode Describes the mode for selecting values.
- SINGLE (List box allows single selection only)
- SINGLE_INTERVAL (List box allows single continuous
selection of options using shift key of keyboard)
- MULTIPLE_INTERVAL (List box allows multiple
selections of options using ctrl key of keyboard)
METHODS DESCRIPTION EXAMPLE
getSelectedV
alue()
Returns the selected value when only a single
item is selected, if multiple items are selected
then returns first selected value. Returns null in
case no item selected.
jList1.getSelectedValue();
isSelectedInd
ex()
Returns true if specified index is selected. Boolean b= jList1.isSelectedindex();
28. LIST CONTROL EXAMPLE
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText(" "+jList1.getSelectedValue());
jTextField2.setText(" "+jList1.getSelectedIndex());
}
QWAP to create the following form. On click of the button the index number
and river name selected from the list should be displayed in the respective
text fields.
NOTE:The index numbers start from zero.
29. COMBO BOX CONTROL
A combo box is a drop down box of
items, used to get one option out of
several given mutually exclusive
options.
Combo box are preferred over radio
button when there are large number of
options.
In such case using radio buttons may
take up a lot of space on the form and it
may also be inconvenient for the user to
select the desired options.
30. COMBO BOX CONTROL
Contd…….
PROPERTY DESCRIPTION
model Contains the values to be displayed.
selectedIndex/selectedItem Contains the index value/selected item of selected option of the control.
METHODS DESCRIPTION EXAMPLE
getSelectedItem() Returns the selected value. jCombobox1.getSelectedItem();
getSelectedIndex() Returns the selected index Boolean b= jCombobox1.getSelectedindex();
setModel() Sets the data model that the combo box
uses to get its list of elements.
jCombobox1.setModel(ComboBoxModel
aModel);
31. COMBO BOX CONTROL EXAMPLE
private void
jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
jTextField1.setText(""+jComboBox1.getSelectedItem());
}
QWAP to create the following form. On click of the button river selected from
the combo box should be displayed in the text field.
CODE OUTPUT SCREEN