How to use Up/Down Arrow Key in Java Tutorial Using Netbeans IDE

0
33

How to use Up/Down Arrow Key in Java Tutorial Using Netbeans IDE

This tutorial is all about How to use Up/Down Arrow Key in Java Tutorial Using Netbeans IDE. In this tutorial you will learn how to select a record in JTable using the Up and Down Arrow Keys in java and automatically setting the data to the textfield. This tutorial uses Key Event Package library, JTable, JTextField Componenents and Netbeans IDE. Please follow all the steps below to complete this tutorial.

How to use Up/Down Arrow Key in Java Tutorial Using Netbeans IDE

 

Create your project by clicking file at the top of your project and then click new project, you can name your project whatever you want. After that you create a class by right clicking the source packages and new java class. You can name your class whatever you want.

And then you need to import the package import com.sun.glass.events.KeyEvent; above your class. The next step is to right click your JTable and click Events / Key / keyReleased , and then copy the code below and paste it inside the keyReleased Event.

  1. if((evt.getKeyCode()==KeyEvent.VK_UP) || (evt.getKeyCode() == KeyEvent.VK_DOWN)){
  2.  
  3. try{
  4.  
  5. int row = productTable.getSelectedRow();
  6.  
  7. String click = (productTable.getModel().getValueAt(row, 0).toString());
  8.  
  9. String sql = "SELECT YourColumnInDatabase FROM YourTable WHERE ColumnName='"+click+"'";
  10.  
  11. pst = conn.prepareStatement(sql);
  12.  
  13. rs = pst.executeQuery();
  14.  
  15. if(rs.next()){
  16.  
  17. //This is the JTextField
  18.  
  19. productNameTxt.setText(rs.getString("product_name"));
  20.  
  21. companyNameTxt.setText(rs.getString("company_name"));
  22.  
  23. productCategoryTxt.setText(rs.getString("product_category"));
  24.  
  25. }
  26.  
  27. }catch(Exception e){
  28.  
  29. JOptionPane.showMessageDialog(null, e);
  30.  
  31. }
  32.  
  33. }

 

Then you’re done. Run your project and see if it works.

 

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: ryansalem221996@gmail.com

Facebook: facebook.com/RyyanSalem

Contact No.: +639098911050

Ryan A. Salem

BSIT Graduate, soon to be MIT.

System Analyst / System Developer

Related topic(s) that you make like:

1.) Createa MySQL Connection in Java
2.) Automated Payroll System using java Netbeans IDE

 

 

Facebook Comments
(Visited 34 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here