Document

advertisement
CHAPTER:08
JAVA IDE PROGRAMMING-III
Prepared By :
VINAY ALEXANDER (विनय अलेक्सजेंड़र)
PGT(CS) ,KV JHAGRAKHAND
Objective
In this presentation you will learn about the
following swing controls i.e. Purpose, their
design time Properties and Run time Methods.
List (jList)
Combo (List+Text Field) (jComboBox)
Timers
Random Number Generation & use
Working with jList control
A List (or List box) is box shaped control
containing list of objects, from which single or
multiple selection can be made.
jList control offers the following featuresA box shaped control capable to displaying a
list of choices (Text or graphics/images).
It Allows single or multiple selection of items
using mouse.
Equipped with built-in scroll bar to view a
large list.
valueChanged() method of ListSelection
Listener is used to handle the JList events
How to add jList control with Frame
A jList can be attached with frame in
following waySelect List control from Swing palette ,
and drag and drop on the frame.
Select the jList control and click on(…)
button of Model property. A dialog box
appears.
Type list of choices in ListModelEditor
dialog box. Finally press OK button.
List Box is displayed on the frame with
typed choices.
int [ ] getSelectedIndices( )
Returns an array of all of the selected
indices, in increasing order, or an
empty if nothing is selected
Adding/Removing Items at run time (Dynamic List)
Step 1. Add a list control to your form.
Step 2. Define its through DefaultListModel
Step3:The following steps should be
followed in the event handler of the button,
where you want to implement the
command.
1. Make a model object asDefaultListModel dlm = (DefaultListModel)
jList1.getModel();
2. Use following command as per requirement// add item in the end of the list//
dlm.addElement(“Apple”);
// Insert item at given index in the list//
dlm.addElementAt(“Apple”,2);
// Remove selected item from the list//
dlm.removeElement(jList1.getSelectedIndex());
// Remove item from given position from the
list//
dlm.removeElementAt(4);
// Remove given item from from the list//
dlm.removeElement(“Banana”);
3. Reflect all changes to the list
jList1.setModel(dlm)
Table: It is responsible for displaying
or representing the data in tabular way
i.e, in row and column format using
JTable. But the data to a Jtable is
provided via a Table Model.
A Table Model is responsible for
storing data that will act as source for
Jtable.
Steps for Adding a row in a table:
1. Create a table in netbeans. if a table
stores details like empno, empname, desig,
basic.
2. create an object array out of the data for
the new row by specifying data in the same
order as determined in step 1
object newRow[]={1105,“ram”, “head”,108000};
Or
object[ ] newRow={ emn.getText,
emname.getText(),
emdesig. getText(),
emsal. getText()
};
3. Obtain the table’s
DefaultTableModel object.
Syntax:
model
DefaultTableModel <identifier>=(DefaultTableModel) <table name>
Example:
in
a
. getMode();
DefaultTableModel tm=(DefaultTableModel) emptbl.getMode();
Import javax.swing.table.*; // top of source editor
4. Now add the object array created in step 2 into the
table-model object of step 3 using addRow( ) method.
Syntax:
<Default-Table-Model-object> .addRow(< object -array-containing row-data> ;
OR
tm.addRow(newRow); // add a row in a table
tm.removeRow(2); // remove row 3 from table
Commonly used methods of Jtable class
Ml getModelethod
Description
int getColumnCount( )
Return the number of column in the table
int getRowCount ( )
Return the number of row in the table
TableMode getModel( )
Return the TableModel that provides the
data displayed by this JTable
Object getValueAt(int row,int column)
Return the cell value at row and column
MENU BASIC
=> Horizontal menu known as Menu bar. It
contains one or more menus and has a
customary ,platform-dependent location.
=> Vertical Menu known as popup
menu/pull-down/ sub menu. It is invisible
until the user performs more mouse action.
=> A Toolbar is a collection of buttons that
correspond to items in an application’s
menu, providing a graphic interface for the
user to access an application’s most
frequently used functions and commands.
Download