codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » JAVA » Article

A simple way to JTable

Article by: Kanad Deshpande (4/26/2005)
Bookmark us now! Add to Favourites
Email a friend! Tell a friend
Summary: Many face trouble while dealing with JTable. Here is simplest way to handle JTable.
Viewed: 209618 times Rating (175 votes): 
 4.1 out of 5
  Rate this Article   Read Comments   Post Comments

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).


Select All Code



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.





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'A simple way to JTable'
RELATED ARTICLES
Java MP3 Player
by David Barron
A fully functioning MP3 Player with complete source code available for download
A simple way to JTable
by Kanad Deshpande
Many face trouble while dealing with JTable. Here is simplest way to handle JTable.
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.
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.
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.








Recent Forum Threads
•  Game:Colonization based with HTML5 Canvas and JavaScript
•  Pointwise.GridGen.v15.18
•  Global.Mapper.v15.2.3.b060614
•  Geometric_Glovius_Pro_v3.6.1
•  VERO.SurfCAM.v2014
•  Schlumberger.Petrel.V2013.2
•  Petrel.V2013.2
•  Altair.HyperWorks.v12
•  VoluMill.v6.1


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2014