A simple way to JTable
While dealing with JTable you come across two main features
1)JTable
2)TableModel
With JTable you can display the data. You can allow the user to edit the data.
The tableModel takes care of the data to be displayed using JTable.
The JTable is normally added in <I>JScrollPane</I>. So when the rows get exceeded display area, the scrollbars of the scrollpane automatically gets activated.
you have to
import javax.swing.table.*;
See Below Code in which there are 4 JButtons
1)Add Row
2)Delete Row
3)GetValue
4)SetValue
Using those buttons you can create a row,delete a row at runtime.
getValue returns value at particular cell while
setValue sets the value at particular cell
plus other important JTable related methods are discussed below.
see the code below (tableExample.java).
In the above code
For rows and columns there are two vectors
rows=new Vector();
columns= new Vector();
columnNames is String array which is added to columns Vector
TableModel deals with data
DefaultTableModel tabModel=new DefaultTableModel();
Now set Vectors for rows and columns to TableModel by
tabModel.setDataVector(rows,columns);
table=new JTable(tabModel);
Note that we implement TableModelListener on TableModel
so as to know when the table is modified at runtime.
As per above code it is now simple that, as we add element to the Vector 'rows' the table rows will get added. To remove table row we can remove item from the vector 'rows'.
First add Columns to the Vector column
Now we will see the simple method to add a row in the table
Select All Code
|
|
The method addRow has Vector r which has return value of method
createBlankElement()
in createBlankElement() method there is Vector with blank no of strings
with same no of columns.
Use of rows.addElement creates table row.
Now we will see how to detect the change in the table cell value.
see the method tableChanged
Select All Code
|
|
In the above method as soon as the tablemodel data is changed
the tableChanged method gets invoked.
you can perform various operations here after the data is changed
in the table.
you can now set the data in the row by using
table.setValueAt(val,row,col) and get value by using table.getValueAt(row,column).
Note that as soon as you use setValueAt the method tableChanged will
get fired.
Now we will see how to delete table Row
see below
Select All Code
|
|
In the above code as you can see remove element at Vector which in
addition will remove the table row.
You can put any row no. to be removed. Here the selected row will be removed.
Same is case for getValueAt() method. Here we getValueAt(0,0) and
we setValueAt(0,2).
Note that if no row is selected or no column is selected then it returns -1
Now letus see some other methods
by using
table.getRowCount();// gives total rows in table
table.getColumnCount();// gives total rows in table
table.getSelectedRow();// gives Selected Row index
table.getSelectedColumn();// gives Selected Column index
table.setRowSelectionAllowed(false);// makes select individual cell
table.setRowSelectionInterval(int ind,int ind1);// select rows from ind to ind1
table.setColumnSelectionInterval(int ind,int ind1);// select Columns from ind to ind1
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// do not adjust table size automatically
table.setRowHeight(int rowIndex,int height);// sets row height for particular row
table.getColumnModel().getColumn(int index).setWidth(int width);// changes column width
table.getColumnModel().getColumn(int index).setHeaderValue"Changed");
//changes column header for the given column at runtime
table.clearSelection();//clear all selection for row and column
table.changeSelection(rowIndex, columnIndex, false,false);//Selects the Cell
table.setShowGrid(boolean)//shows grid
table.setShowHorizontalLines(boolean)// shows Horizontal Lines
table.setShowVerticalLines(boolean)//shows Vertical Lines
table.selectAll();//Selects all table
----
In general JTables can be used in applications where user has to enter
detailed transaction part.Easy to use for the user. You can add columns
to table as per requirement.
|
|