Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vikas pandey
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
1 Answer(s)      2 years and 4 months ago
Posted in : Swing AWT


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


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

 

 

DMCA.com