Guest User

Untitled

a guest
Jan 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. Fetch data from database into Jtextfield when One Jtextfield is Typed
  2. txtNo.addKeyListener(new KeyAdapter() {
  3. public void keyTyped(KeyEvent ke) {
  4. Connection conn = null;
  5. try{
  6. Class.forName("oracle.jdbc.driver.OracleDriver");
  7.  
  8. conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
  9. Statement st = conn.createStatement();
  10. String load = "Select * from Store_info_table where PART_NUMBER = '" + txtNo.getText() + "'";
  11. ResultSet rs = st.executeQuery(load);
  12. while(rs.next()){
  13. txtName.setText(rs.getString("SPARE_DESC"));
  14. }
  15. }catch(Exception ae){
  16.  
  17. }
  18. }
  19. });
  20.  
  21. import java.awt.event.*;
  22. import javax.swing.*;
  23.  
  24. public class Example extends Box{
  25.  
  26. JLabel txtName = new JLabel("Nothing Entered");
  27.  
  28. public temp(){
  29. super(BoxLayout.Y_AXIS);
  30. // Add FocusListener to first field
  31. final JTextField txtNo = new JTextField(20);
  32. txtNo.addFocusListener(new CustomFocusListener(txtNo));
  33. add(txtNo);
  34.  
  35. // Add TextListener to first field
  36. final JTextField txtNo2 = new JTextField(20);
  37. txtNo2.addFocusListener(new CustomFocusListener(txtNo2));
  38. add(txtNo2);
  39.  
  40. // Add TextListener to first field
  41. final JTextField txtNo3 = new JTextField(20);
  42. txtNo3.addFocusListener(new CustomFocusListener(txtNo3));
  43. add(txtNo3);
  44.  
  45. add(new JButton("Do Something"));
  46.  
  47. add(txtName);
  48.  
  49. }
  50.  
  51.  
  52. public static void main(String[] args){
  53. final JFrame frame = new JFrame();
  54. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55. frame.add(new Example());
  56. frame.pack();
  57. frame.setLocationRelativeTo(null);
  58. frame.setVisible(true);
  59. }
  60.  
  61. /**
  62. * Your custom focust listener that does all your SQL Work
  63. */
  64. public class CustomFocusListener extends FocusAdapter{
  65. JTextField field;
  66.  
  67. public CustomFocusListener(JTextField field){
  68. this.field = field;
  69. }
  70.  
  71. @Override
  72. public void focusLost(FocusEvent e) {
  73. //Assuming your database connection works, this gives you an example to follow
  74. txtName.setText(field.getText());
  75. /*Connection conn = null;
  76. try{
  77. Class.forName("oracle.jdbc.driver.OracleDriver");
  78.  
  79. conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
  80. Statement st = conn.createStatement();
  81. String load = "Select * from Store_info_table where PART_NUMBER = '" + field.getText() + "'";
  82. ResultSet rs = st.executeQuery(load);
  83. while(rs.next()){
  84. txtName.setText(rs.getString("SPARE_DESC"));
  85. }
  86. }catch(Exception ae){
  87.  
  88. }*/
  89. }
  90. }
  91. }
  92.  
  93. public class TestForm02 {
  94.  
  95. public static void main(String[] args) {
  96. new TestForm02();
  97. }
  98.  
  99. public TestForm02() {
  100. EventQueue.invokeLater(new Runnable() {
  101.  
  102. @Override
  103. public void run() {
  104.  
  105. try {
  106. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  107. } catch (ClassNotFoundException ex) {
  108. } catch (InstantiationException ex) {
  109. } catch (IllegalAccessException ex) {
  110. } catch (UnsupportedLookAndFeelException ex) {
  111. }
  112.  
  113. JFrame frame = new JFrame();
  114. frame.setLayout(new BorderLayout());
  115. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  116. frame.add(new MyForm());
  117. frame.pack();
  118. frame.setLocationRelativeTo(null);
  119. frame.setVisible(true);
  120.  
  121. }
  122. });
  123. }
  124.  
  125. protected class MyForm extends JPanel {
  126.  
  127. private JTextField txtNo;
  128. private JTextField txtName;
  129. private String partToLoad;
  130. private Timer loadTimer;
  131.  
  132. public MyForm() {
  133.  
  134. setLayout(new GridBagLayout());
  135. GridBagConstraints gbc = new GridBagConstraints();
  136. gbc.gridx = 0;
  137. gbc.gridy = 0;
  138. gbc.insets = new Insets(2, 2, 2, 2);
  139. gbc.anchor = GridBagConstraints.WEST;
  140.  
  141. txtName = new JTextField(12);
  142. txtNo = new JTextField(12);
  143. txtName.setEnabled(false);
  144.  
  145. add(new JLabel("Parts #:"), gbc);
  146. gbc.gridx++;
  147. add(txtNo, gbc);
  148.  
  149. gbc.gridy++;
  150. gbc.gridx = 0;
  151. add(new JLabel("Description:"), gbc);
  152. gbc.gridx++;
  153. add(txtName, gbc);
  154.  
  155. txtNo.addFocusListener(new FocusAdapter() {
  156. @Override
  157. public void focusLost(FocusEvent e) {
  158. loadParts();
  159. }
  160. });
  161.  
  162. txtNo.getDocument().addDocumentListener(new DocumentListener() {
  163. protected void update() {
  164. loadTimer.restart();
  165. }
  166.  
  167. @Override
  168. public void insertUpdate(DocumentEvent e) {
  169. update();
  170. }
  171.  
  172. @Override
  173. public void removeUpdate(DocumentEvent e) {
  174. update();
  175. }
  176.  
  177. @Override
  178. public void changedUpdate(DocumentEvent e) {
  179. update();
  180. }
  181. });
  182.  
  183. loadTimer = new Timer(250, new ActionListener() {
  184. @Override
  185. public void actionPerformed(ActionEvent e) {
  186. loadParts();
  187. }
  188. });
  189. loadTimer.setRepeats(false);
  190. loadTimer.setCoalesce(true);
  191.  
  192. }
  193.  
  194. protected void loadParts() {
  195.  
  196. String text = txtNo.getText();
  197. // Don't want to trigger this twice...
  198. if (text == null ? partToLoad != null : !text.equals(partToLoad)) {
  199. partToLoad = text;
  200. txtNo.setEnabled(false);
  201. txtName.setEnabled(false);
  202. BackgroundWorker worker = new BackgroundWorker();
  203. worker.execute();
  204. }
  205.  
  206. }
  207.  
  208. protected class BackgroundWorker extends SwingWorker<String, String> {
  209.  
  210. @Override
  211. protected String doInBackground() throws Exception {
  212.  
  213. // Do you're database load here. Rather then updating the text
  214. // field, assign it to variable and return it from here
  215.  
  216. String desc = "Some description"; // load me :D
  217.  
  218. // Inserted delay for simulation...
  219. Thread.sleep(2000);
  220.  
  221. return desc;
  222.  
  223. }
  224.  
  225. @Override
  226. protected void done() {
  227. try {
  228. String value = get();
  229. txtName.setText(value);
  230.  
  231. txtName.setEnabled(true);
  232. txtNo.setEnabled(true);
  233. } catch (InterruptedException exp) {
  234. exp.printStackTrace(); // Log these some where useful
  235. } catch (ExecutionException exp) {
  236. exp.printStackTrace(); // Log these some where useful
  237. }
  238. }
  239. }
  240. }
  241. }
  242.  
  243. txtNo.addFocusListener(new FocusAdapter() {
  244. public void focusLost(FocusEvent e) {
  245. Connection conn = null;
  246. try{
  247. Class.forName("oracle.jdbc.driver.OracleDriver");
  248.  
  249. conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
  250. Statement st = conn.createStatement();
  251. String load = "Select * from Store_info_table where PART_NUMBER = '" + txtNo.getText().trim() + "'";
  252. ResultSet rs = st.executeQuery(load);
  253. while(rs.next()){
  254. txtName.setText(rs.getString("SPARE_DESC"));
  255.  
  256. }
  257. }catch(Exception ae){
  258.  
  259. }
  260. }
  261. });
Add Comment
Please, Sign In to add comment