Results 1 to 1 of 1
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default About focusLost and keyTyped, need feedback

    Hi, here again :)
    As I always saying I am learning java again. This is not so 'critical' but I want know if I did the right thing.

    My aim is to limit calling methods that is not needed yet.
    For example: I have a 3 JTextField named text1, text2, text3 which will query a SELECT STATEMENT where values is equal to the text1, text2 and text3. Now if the query is already called and the user click on one of the text1, text2 and text3 then click to another JComponent this means that focusLost for that Jtextfield will fire, and query a SELECT STATEMENT again. This is what I am trying to eliminate.

    What I did is..
    Java Code:
        1. Declare a private (3)Strings and (1)Boolean.
        2. When user type on the JTextField, if boolean is false then it will compare the stored value of JTextField and its current value.
        3. If stored value and current value of JTextfield is equals then Boolean is true, if not then false
        4. At focusLost of the JTextField, if Boolean is true then we need to call the SELECT STATEMENT, if False then do nothing.
    
        Hope I explain it good. :)
    The code is here:
    Java Code:
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
    
        public class frmRequest extends javax.swing.JFrame implements FocusListener, KeyListener{
                String req_text1 = "";  //Save last value of text1 component (JTextField).
                String req_text2 = "";  //Save last value of text2 component (JTextField).
                String req_text3 = "";  //Save last value of text3 component (JTextField).
                Boolean req_need_to_requery = true;
    
                int requeryCount = 0; //just add this here in replace of query result.
    
                JTextField text1 = new JTextField("Text#1");
                JTextField text2 = new JTextField("Text#2");
                JTextField text3 = new JTextField("Text#3");
                JLabel label1 = new JLabel();
                JLabel label2 = new JLabel();
                JPanel p = new JPanel();
    
                public frmRequest()
                {
                        this.add(p);
                        p.setLayout(new BorderLayout());
                        p.add(text1, BorderLayout.PAGE_START);
                        p.add(label1, BorderLayout.PAGE_START);
                        p.add(text2, BorderLayout.LINE_START);
                        p.add(label2, BorderLayout.LINE_END);
                        p.add(text3, BorderLayout.PAGE_END);
                        pack();
    
                        text1.addFocusListener(this);
                        text2.addFocusListener(this);
                        text3.addFocusListener(this);
    
                        text1.addKeyListener(this);
                        text2.addKeyListener(this);
                        text3.addKeyListener(this);
    
                        this.setVisible(true);
                }
    
                void requeryEverything()
                {	requeryCount = requeryCount + 1;
                        label1.setText("Click " + requeryCount);
                        label2.setText("Click " + requeryCount);
                }
                public void focusGained(FocusEvent ek){}
    
                public void focusLost(FocusEvent ek)
                {  	if(req_need_to_requery == true)
                        { requeryEverything();
                           req_need_to_requery = false;
                        }
                }
    
                public void keyTyped(KeyEvent ek)
                {	if(ek.getComponent() == text1)
                        {	if(req_need_to_requery == false)
                                {   	if(req_text1.equals(text1.getText()))
                                                {req_need_to_requery = false;}
                                                else  {req_need_to_requery = true;
                                                 req_text1 = text1.getText();
                                                }
                                        }
                        }
    
                        if(ek.getComponent() == text2)
                        {	if(req_need_to_requery == false)
                                {   	if(req_text2.equals(text2.getText()))
                                                {req_need_to_requery = false;}
                                                else  {req_need_to_requery = true;
                                                 req_text2 = text2.getText();
                                                }
                                        }
                        }
    
                        if(ek.getComponent() == text3)
                        {	if(req_need_to_requery == false)
                                {   	if(req_text3.equals(text3.getText()))
                                                {req_need_to_requery = false;}
                                                else  {req_need_to_requery = true;
                                                 req_text3 = text3.getText();
                                                }
                                        }
                        }	}
    
                public void keyPressed(KeyEvent ek){}
    
                public void keyReleased(KeyEvent ek){}
    
                public static void main(String[] args)
                {	new frmRequest().setVisible(true);
                }
        }
    Thanks for you time,
    geje
    Last edited by mine0926; 06-26-2010 at 11:10 AM.

Similar Threads

  1. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 11:09 PM
  2. Char comparison/assigning through keyTyped
    By AdamMurg in forum Java Applets
    Replies: 0
    Last Post: 10-21-2009, 06:42 PM
  3. Netbeans - focusGained - focusLost
    By dbashby in forum New To Java
    Replies: 1
    Last Post: 04-15-2009, 11:17 PM
  4. website section feedback
    By MuslimCoder in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-25-2009, 08:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •