This tutorial entitled “Display Data from Selected Table Row to another Form in Java” will help you on how your program display information from selected Table Row and display it to another form in Java using any element that support text.
To complete this tutorial, you are required to learn and understand my last topic about “How to Load Data from MySQL Database to Table Element in Java”. Please follow all the steps to complete this tutorial.
Display Data from Selected Table Row to another Form in Java Steps
- Add a new button inside your form that loads data from MySQL Database.
2. The form should look like the image below.
3. Double click the button and insert the ff. codes below.
[java]int row=jTable1.getSelectedRow();
String id = (jTable1.getModel().getValueAt(row, 0).toString());
DataUpdate du = new DataUpdate();
du.loadInformation(id);
du.setVisible(true);[/java]
DataUpdate – the name of your form create in Step 4, loadInformation – the name of the method inserted in step 6, jTable1 – the name of your Table.
4. Add/Insert a new JFrame Form into your project.
5. Design your form just like what we do in our Save Data tutorial.
6. Inside your code view, insert the ff. codes below inside your class.
[java]public void loadInformation(String id){
try{
String sql=”SELECT * FROM userinfo WHERE idUserInfo='” + id + “‘”;
pst=conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
while(rs.next())
{
jTextField1.setText(rs.getString(“FirstName”));
jTextField2.setText(rs.getString(“LastName”));
jTextField3.setText(rs.getString(“Address”));
jTextField4.setText(rs.getString(“Contact”));
jTextField5.setText(rs.getString(“Email”));
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}[/java]
7. Run your load data form and the output should look like the image below. After I click the update button, the new Create form displayed together with the information selected from load data table.
About The Display Data from Selected Table Row to another Form In Java
Project Name: | Display Data from Selected Table Row to another Form |
Language/s Used: | JAVA |
Database: | MySQL |
Type: | Desktop Application |
Developer: | IT SOURCECODE |
Updates: | 0 |
Related Articles You May Like: