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.....
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:
Advertisements
Remove JTable row that read txt file recordsRemove
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 swingjtable insert
row swing
How to insert and refresh
row in
JTable?
Inserting Rows in a
JTable example
How to retrieve single row from MSSQLHow 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 MSSQLHow 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 MSSQLHow 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 databasehow 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 dynamicallyjtable-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 - SQLSql
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 valuesArraylist 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
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 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 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 rowdelete 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_idrow_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_idrow_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
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
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 get table row contents into next jsp pageHow 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-ServletADD 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 dynamicallyHoe 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