How to read and retrieve jtable row values into jtextfield on clicking at particular row ...

How to read and retrieve jtable row values into jtextfield on clicking at particular row ...

Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read all the values of particular row at which mouse is clicked. and display that in textfield below the jtable.

plz sir give me the code....

Thank you Sir.....

View Answers

July 28, 2012 at 5:51 PM

Here is a code that read database values and store into jtable and retrieve jtable row values into jtextfield on clicking at particular row.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.event.*;

class DisplayTableAction{
int counter1=0,counter2=0,counter3=0,counter4=0;
int counter=0;
public static void main(String arg[]){
DisplayTableAction action=new DisplayTableAction();
}
DisplayTableAction(){
ListSelectionModel listSelectionModel;
final Vector columnNames = new Vector();
final Vector data = new Vector();
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from person");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()){
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
st.close();
}
catch(Exception e) {}
JFrame frame=new JFrame();
frame.setLayout(null);
final JLabel lab1=new JLabel("ID");
final JTextField text1=new JTextField(20);

final JLabel lab2=new JLabel("Name");
final JTextField text2=new JTextField(20);

final JLabel lab3=new JLabel("Address");
final JTextField text3=new JTextField(20);

final JLabel lab4=new JLabel("Email");
final JTextField text4=new JTextField(20);
lab1.setBounds(150,230,100,20);
text1.setBounds(270,230,100,20);

lab2.setBounds(150,260,100,20);
text2.setBounds(270,260,100,20);

lab3.setBounds(150,290,100,20);
text3.setBounds(270,290,100,20);

lab4.setBounds(150,320,100,20);
text4.setBounds(270,320,100,20);


final JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(150,100,400,100);
frame.add(lab1);
frame.add(text1);

frame.add(lab2);
frame.add(text2);

frame.add(lab3);
frame.add(text3);

frame.add(lab4);
frame.add(text4);
frame.add( scrollPane );
frame.setVisible(true);
frame.setSize(800,500);

lab1.setVisible(false);
text1.setVisible(false);

lab2.setVisible(false);
text2.setVisible(false);

lab3.setVisible(false);
text3.setVisible(false);

lab4.setVisible(false);
text4.setVisible(false);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){

Object obj1=null,obj2=null,obj3=null,obj4=null;
String q="";
int i=table.getSelectedRow();
if(i==0){
counter1++;
q=Integer.toString(counter1);
}
if(i==1){
counter2++;
q=Integer.toString(counter2);
}
if(i==2){
counter3++;
q=Integer.toString(counter3);
}
if(i==3){
counter4++;
q=Integer.toString(counter4);
}
obj1 = GetData(table, i, 0);
obj2 = GetData(table, i, 1);
obj3 = GetData(table, i, 2);
obj4 = GetData(table, i, 3);


lab1.setVisible(true);
text1.setVisible(true);

lab2.setVisible(true);
text2.setVisible(true);

lab3.setVisible(true);
text3.setVisible(true);

lab4.setVisible(true);
text4.setVisible(true);
text1.setText(obj1.toString());
text2.setText(obj2.toString());
text3.setText(obj3.toString());
text4.setText(obj4.toString());


}
});

}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}









Related Tutorials/Questions & Answers:
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...  Hello Sir, I am developing a desktop... to read all the values of particular row at which mouse is clicked. and display
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
Advertisements
Remove JTable row that read txt file records
Remove JTable row that read txt file records  Hi every one. i have a jtable that correctly read data frome file and show them in own. I want to add a "Delete" button that when select a row and clicked button, row must deleted
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
How to retrieve single row from MSSQL
How to retrieve single row from MSSQL  Hello sir, Sir i... giving the name of both patient & doctor,after clicking the search button.It... mean only his row has to be displayed in the next Jpanel within same window.I
How to retrieve single row from MSSQL
How to retrieve single row from MSSQL  Hello sir, Sir i... giving the name of both patient & doctor,after clicking the search button.It... mean only his row has to be displayed in the next Jpanel within same window.I
How to retrieve single row from MSSQL
How to retrieve single row from MSSQL  Hello sir, Sir i... giving the name of both patient & doctor,after clicking the search button.It... mean only his row has to be displayed in the next Jpanel within same window.I
how update JTable after adding a row into database
how update JTable after adding a row into database  J have two... in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djile pak.java
jtable-adding a row dynamically
jtable-adding a row dynamically  hi..i am doing a project for pharmacy .. *pblm:* when i want to enter the details in jtable while running... to add one more row dynamically as a new row 4th if the details are large.but every
Sql row retrieve - SQL
Sql row retrieve  Sir What are the sql statements so that I can retrieve rows from a table by specifying the row numbers such as row no.5 to 10, row no.13 to 20 like that. thanks Pabitra kr debanth. Guwahati. 
Java: Adding Row in JTable
Java: Adding Row in JTable   how about if we already have the JTAble created earlier. And i just found nothing to get its DefaultTableModel, thus, I can't call insertRow() method. Is there any work around for this? I found
Arraylist from row values
Arraylist from row values  Hello, can anyone please help on how to make an arraylist from the row values of a particular column from a database...; class Retrieve{ public static void main(String[] args){ try
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
how to read the values for text and csv files and store those values... file.it was stored in perticular directory now i have to read the stored(.csv or .txt) file and get the values from that file and store them into database
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
pyspark dataframe drop null - how to drop row with null values
pyspark dataframe drop null - how to drop row with null values  Hi, I have a data frame with following values: Name,address,age I want to drop all the rows having address is NULL. how to do this? thanks   Hi, you
Enabling Row, Column and Cell Selections in a JTable
to describe how to enable the row, column and cell selections in a JTable... Enabling Row, Column and Cell Selections in a JTable... will see the enabling row, column and cell selections in a JTable. When you
Removing a Row from a JTable
Removing a Row from a JTable       After inserting the data in a JTable, if you wish... the JTable. For removing the data of row from JTable, you will remove it from
how to make seperate view count for each row while clicking view button
how to make seperate view count for each row while clicking view button  I am getting problem with view count when i click view button the seperate count should be happen for each row here is my code ** Tabledata.jsp <
show folder on server by clicking a row in table using jsp and ajax
show folder on server by clicking a row in table using jsp and ajax  Hi, i want a jsp page which has one dropdown and one textfield. when i click... display the table. Now if i click on a row it should open a folder in server
show folder on server by clicking a row in table using jsp and ajax
show folder on server by clicking a row in table using jsp and ajax  Hi, i want a jsp page which has one dropdown and one textfield. when i click... display the table. Now if i click on a row it should open a folder in server
How to Dragging and dropping HTML table row to another position(In Jsp) and save the new position (values) also in database(MySql)?
How to Dragging and dropping HTML table row to another position(In Jsp) and save the new position (values) also in database(MySql)?  Hi members, I have one Html table in jsp page and i am iterating values (in columns of html
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have to update the cell values from there only means that whatever values i ma entering
Query to insert values in the empty fields in the last row of a table in Mysql database?
Query to insert values in the empty fields in the last row of a table in Mysql... row of my MYSQL database. Now I want to fill up those empty fields in the last row. So what will be the query
Delete a row from database by id
) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row...Delete a row from database by id  I m creating a small application... all the values are added in to database and page is redirected to a new page
delete row
delete row  how to delete row using checkbox and button in php... of table. When the user selects the particular checkbox, that row will get deleted...("sourabh", $link); $rows=mysql_query("select * from sonu"); $row=mysql
java jtable
in which i have to display database records in jtable .now I want to read only the values of particular row at which the jtextfield has been set. and display that in jtable. plz help me with the code
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
How to insert rows in jTable?
How to insert rows in jTable?  Hi, I need to take input from user using JTable. I want an empty row to appear after clicking a insert button. The values will be entered in this empty row. I have searched on this but could
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I am getting the data's from the table that was stored in database. Now... as for example Approve then it must select all the data in the row and send
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I am getting the data's from the table that was stored in database. Now... as for example Approve then it must select all the data in the row and send
How to delete the row from the Database by using servlet
How to delete the row from the Database by using servlet  Dear Sir... 25 users details are there. I am given 6th (some n th row)user details true values even though while loop is not terminate, it checking until the end
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... .when i enter search value in the search field only one row is displayed in the jtable .Suppose i enter name in search field which has two rows in the database
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... .when i enter search value in the search field only one row is displayed in the jtable .Suppose i enter name in search field which has two rows in the database
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... .when i enter search value in the search field only one row is displayed in the jtable .Suppose i enter name in search field which has two rows in the database
insert and delete a row programmatically
insert and delete a row programmatically  How to insert and delete a row programmatically ? (new feature in JDBC 2.0
didSelectRowAtIndexPath deselect row
didSelectRowAtIndexPath deselect row  Hi, In my UITableView application when user taps a row, how to deselect row? Thanks
how to update specific row in on update key in the Navicat for mysql trigger
how to update specific row in on update key in the Navicat for mysql trigger   Blockquote insert into two(name, date) select name, curdate() from one on duplicate key update name=values(name
ModuleNotFoundError: No module named 'row'
ModuleNotFoundError: No module named 'row'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'row' How to remove the ModuleNotFoundError: No module named 'row' error
ModuleNotFoundError: No module named 'row'
ModuleNotFoundError: No module named 'row'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'row' How to remove the ModuleNotFoundError: No module named 'row' error
Dragging and dropping HTML table row to another position(In Jsp) and save the new position (values) also in database
Dragging and dropping HTML table row to another position(In Jsp) and save the new position (values) also in database  Hi members, I have one Html table in jsp page and i am iterating values (in columns of html table)from
how to delete a row in sql without using delete command.
how to delete a row in sql without using delete command.  how to delete a row in sql without using delete command. thanks in advance
How to add dropdown list in a row of a sort table applet?
How to add dropdown list in a row of a sort table applet?  How to add dropdown list in a row of a sort table applet
Getting Cell Values in a JTable
value  at a specified row and column position in JTable. The cell values... Getting Cell Values in a JTable     ... values in a JTable component. It is a same as setting the cell values in a JTable
Setting Cell Values in JTable
Setting Cell Values in JTable     ... values in JTable component. For this you must have the some previous knowledge about JTable. A cell is known as the format of  a row and a column in 
How to Manipulate List to String and print in seperate row
How to Manipulate List to String and print in seperate row  I am... header row BufferedWriter out = new BufferedWriter(new FileWriter(filename...,UserName,WorkforceID,Organization dthomas,dthomas001,12345,Finance,HR How do i
How to search the selected item in row table using radia button in JSP?
How to search the selected item in row table using radia button in JSP?  How to search the selected item in row table using radia button in JSP
How to get table row contents into next jsp page
How to get table row contents into next jsp page  Hi, I have a 30... page.But the problem is how do i get the selected radio button table row... code retrieve data from database and display in the html table. At each row
ADD ROW - JSP-Servlet
ADD ROW  Hi Sir, How to use add row and delete row concept in jsp .  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/add-element.shtml Thanks
Hoe to refresh a table row dynamically
Hoe to refresh a table row dynamically  Want to refresh a table data when a particular link is clicked. How can i do using ajax ?   Hello... will able to refresh a particular table row?ADS_TO_REPLACE_1 Please clarify

Ads