Home » JAVA » Article
A simple way to JTable
|
| Article by: | Kanad Deshpande (4/26/2005) |
|
| Sponsored by: | FindMyHosting - Web Hosting Search |
| Summary: | Many face trouble while dealing with JTable. Here is simplest way to handle JTable. |
|
| Viewed: 21893 times |
Rating (80 votes): |
|
3.5 out of 5 |
|
|
|
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
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
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
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.
|
|
View highlighted Comments
User Comments on 'A simple way to JTable'
|
Posted by :
m_ade_f at 03:11 on Thursday, April 28, 2005
|
This is good articel. I want to try it. Thanks.
| |
Posted by :
kanad at 04:07 on Friday, May 06, 2005
|
Additional features that I would like to add
table.setPreferredScrollableViewportSize(new Dimension(table.WIDTH, 75)); to set PreferredScrollableViewportSize
---
To disable reordering
table.getTableHeader().setReorderingAllowed(false);
---
To disable resizing
table.getTableHeader().setResizingAllowed(false);
---
To make table rows of table 'uneditable'
table = new JTable(tabModel)
{
public boolean isCellEditable(int row, int column)
{
return false;
}
};
This will make the table uneditable at runtime.
-----
To add a Component to particular column
JComboBox cmbColor = new JComboBox();
cmbColor.addItem("Red");
cmbColor.addItem("Green");
cmbColor.addItem("Blue");
TableColumn colColumn = table.getColumnModel().getColumn(0);
colColumn.setCellEditor(new DefaultCellEditor(cmbColor));
---
| |
Posted by :
yogeshbhagare at 02:49 on Tuesday, May 10, 2005
|
this is very simple kanad
i like it.
| |
Posted by :
yogeshbhagare at 02:55 on Tuesday, May 10, 2005
|
u made it possible.
| |
Posted by :
ramanuj at 02:53 on Thursday, May 19, 2005
|
u made it so easy & understandale
| |
Posted by :
sankarreddy at 06:04 on Wednesday, July 27, 2005
|
NICE ONE
| |
Posted by :
sshivaprasad at 08:55 on Thursday, September 08, 2005
|
Really good
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
A simple way to JTable by Kanad Deshpande
Many face trouble while dealing with JTable. Here is simplest way to handle JTable. |
 |
Java MP3 Player by David Barron
A fully functioning MP3 Player with complete source code available for download |
 |
Understanding Hibernate ORM for Java/J2EE by Saritha.S.V
Hibernate is the most popular and most complete open source object/relational mapping solution for Java environments.Hibernate's goal is to relieve the developer from 95 percent of common data persistence related programming tasks. |
 |
ID Verification using JSP by Nouman Rashid
One of the most important parts of
web development is to make sure that
only authorized users get access to certain areas of the site. This tutorial
takes a look at various steps involved
in making JSP pages which validate a
user ID and password from a MS Access database which contains the username and
password.
|
 |
Java Native Interface (JNI) by Kanad Deshpande
Java Native Interface (JNI) is one of the intersting interface by java
By using Java Native Interface (JNI) you can operate with other applications and libraries. |
 |
Login codes with JSP,JavaBean from mySQL database by Prakash
my problem is can i have the code to login with the username and password using JSP and JavaBean/Servlets from mySQL database.When the user enters the username and password in the login page then it will go to the requested site.How to do it? |
 |
Java Speech Synthesizer by David Barron
Small and simple. Type a sentence and press enter and your computer will speek to you. |
 |
simple Java Development Environment by David Barron
Program in JAVA with ease, using this development environment, or adapt it to your own needs. |
 |
Turn EJB components into Web services by Krunal J Patel
Web services have become the de facto standard for communication among applications. J2EE 1.4 allows stateless Enterprise JavaBeans (EJB) components to be exposed as Web services via a JAX-RPC (Java API for XML Remote Procedure Call) endpoint, allowing EJB applications to be exposed as Web services. This article presents a brief introduction to JAX-RPC, outlines the steps for exposing a stateless session bean as a Web service, and provides the best practices for exposing EJB components as Web services |
 |
CORBA Technology by Krunal Patel
CORBA defines an architecture for distributed objects. The basic CORBA paradigm is that of a request for services of a distributed object. Everything else defined by the OMG is in terms of this basic paradigm.
|
 |
| |