-1

my question is when i insert the first name in jtextfield then corresponding last name of this record show automatically in another jtextfield?

it means i dont want insert same data again and again..once i insert the record in database the next time same record inserted automatically in next form

so anyone here to give me snippet? Thanks in advance

Java D
  • 436
  • 1
  • 8
  • 18

2 Answers2

3

@MirroredFate (to comments in another answer here) never to use KeyListener, use DocumentListener, then there you can to determine all possible users inputs types inside JTextComponents

enter image description here

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextLabelMirror {

    private JPanel mainPanel = new JPanel();
    private JTextField field = new JTextField(20);
    private JTextField field1 = new JTextField(20);

    public TextLabelMirror() {
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            private void updateLabel(DocumentEvent e) {
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        field1.setText(field.getText());
                    }
                });
            }
        });

        mainPanel.setLayout(new GridLayout(1, 0, 10, 0));
        mainPanel.add(field);
        mainPanel.add(field1);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("TextLabelMirror");
        frame.getContentPane().add(new TextLabelMirror().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    @mKorbel..but this is the mirror label but i want data from database which is the related to the inserted first name? – Java D Apr 05 '13 at 06:57
  • be sure that this is reason for why your question isn't answered, this question is about logics, not about code – mKorbel Apr 05 '13 at 07:04
  • @mKorbel..yes u r right but i want help from you thatswhy i asked you..otherwise its okay – Java D Apr 05 '13 at 07:14
  • @kapil As said in my first comment, the question is to vauage. How can we possible help beyond the basics when we don't your database or it's structure? – MadProgrammer Apr 05 '13 at 09:02
  • @MadProgrammer..so i have one table which have 4 columns.. named Firstname,Lastname,Address,City so that when i want to print anything to whom i want to give report..i just enter his first name according to this corresponding value will be fetched into the another jtextfield.. not complicated at all.. – Java D Apr 05 '13 at 09:12
0

You really want to check out this documentation: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Your code will look something like this:

JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
KeyListener kl = MyCustomKeyListener();
firstName.addKeyListener(kl);

In your custom KeyListener, override the keyTyped() method to find the value of firstName, look it up in the database to find the corresponding last name, then use it to set the value of lastName.

This way you can make it so that every time a key is typed this will execute, or you can check and make sure only a specific key triggers action (like pressing return).

Also, I should mention you can use an anonymous class for this. Instead of creating MyCustomKeyListener, you can say:

firstName.addKeyListener(new KeyListener(){
    void keyTyped(keyEvent e){
        //whatever you want to happen when the key is typed in here
    }
});

EDIT: As it has been pointed out, it may be better to use a DocumentListener. This is done by accessing the underlying JTextField's document and adding the listener to that, as shown in the answer here: Value Change Listener to JTextField

Community
  • 1
  • 1
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • *"You really should check out key listeners"* - No, you shouldn't, really. A `DocumentListener` would be far more useful and would include the case where the user pastes text into the field as well, which `KeyListener` won't - Plus, hitting the database on each key stroke is probably not a good idea, as it could "stagger" the application and make it appear to be running slow or not responding – MadProgrammer Apr 05 '13 at 06:16
  • A `DocumentListener` could be useful, too. It seems to me that understanding the entire observer design pattern would be good. I think stating it (DocumentListener) would be "far more useful" and saying "No, you really shouldn't" to a suggestion to read some documentation is rather ridiculous. Also, not "staggering" the application is why I stated that you could check for specific keys rather than every keystroke. – MirroredFate Apr 05 '13 at 06:25
  • You may be right, but generally when people think `KeyListener` is a good idea, it most often isn't and represents a good number of Swing related questions on SO. The preferred mechanism for monitoring `JTextComponent`s is using something like an `ActionListner` or `DocumentListener` (along with `InputVerifier`). I'm sorry if I sound terse, it's not meant as a criticism against you, but as a highlight of a better approach – MadProgrammer Apr 05 '13 at 06:29
  • @MirroredFate not DocumentListener could be used in all cases, please how your KeyListener to determine that String is inserted, selection two of more chars are removed .... etc, please to descibe about KeyListener only in the case that you needed to listennig for three or more Keys are pressed – mKorbel Apr 05 '13 at 06:32
  • delete this answer or change description and used Listener – mKorbel Apr 05 '13 at 06:33
  • @MadProgrammer It could very well be that `DocumentListener` would be the better route to go in this case. I will update my answer to reflect that option. From his question, it seemed as if KeyListener was appropriate. I think the most important thing, though, is seeing an example of the observer design pattern. @mKorbel I'm sorry, but I have no idea what you are saying. – MirroredFate Apr 05 '13 at 06:39
  • 'firstName.addKeyListener(new KeyListener(){ void keyTyped(keyEvent e){ //whatever you want to happen when the key is typed in here } });' where should i put this code?@MirroredFate – Java D Apr 05 '13 at 06:40
  • I have no examples of your code, so I have no idea specifically. However, it should be somewhere around where you are instantiating them. – MirroredFate Apr 05 '13 at 06:43
  • @MirroredFate..so now can you help me to fetch data from database with the help of mKorbel's post? – Java D Apr 05 '13 at 06:58
  • Dude, I have no idea what kind of database you are using or how you are accessing it or what any of your code looks like. I think the question you asked here has been answered, and not just by the people here. If you are trying to figure out how to connect Java to a MySQL database that's an entirely different question. My suggestion for that, btw, would be to look into Hibernate. But it all really depends on what you are trying to do and why. Also, remember: Google is your friend. – MirroredFate Apr 05 '13 at 07:01
  • @MirroredFate..no no hibernate only simple swing application no framework used in this app..yes i m trying to fetch from mysql..if u have idea then share with us.. – Java D Apr 05 '13 at 07:06