1

I'm facing problem with the simulation of the button click. I have JTextField that takes data from scanner (like in grocery stores). After that i need to push enter button, to activate function jTextField1ActionPerformed. Is there any way to do it without producing any button actions?

Or i need tip how to jButton1.doClick() every time jTextField is changed. Please help!:)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {

        AddEmployee.add(jTextField1.getText());
        jLabel7.setText(AddEmployee.name);
        jLabel12.setText(AddEmployee.dep);
        jLabel10.setText(AddEmployee.pos);
        jLabel11.setText(AddEmployee.time);
        num++;
        jName.append("\n"+Integer.toString(num)+". "+AddEmployee.name);
        jTime.append("\n"+Integer.toString(num)+". "+AddEmployee.time);

        jTextField1.setText("");
    } 
    catch (ClassNotFoundException | SQLException | ParseException ex) {
        Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
    }  
}
alex
  • 2,464
  • 23
  • 32
Medet Koilybay
  • 111
  • 2
  • 9
  • Do you find your answer here ? http://stackoverflow.com/questions/4419667/detect-enter-press-in-jtextfield – Eric David Nov 14 '14 at 10:26
  • This is my main point, program has to work without typing enter) – Medet Koilybay Nov 14 '14 at 10:29
  • So you want to trigger the jButton1 immediatly after the jTextField1 is filled ? If so, you don't need a listener to do that, just trigger the button at the same time you fill the text field... – Eric David Nov 14 '14 at 10:34
  • Text is filled by scanner. I'm not doing it manually. That's why i'm asking. It'll be too ez to do in your way) – Medet Koilybay Nov 14 '14 at 10:42
  • See [Perform action upon completion of barcode scanning to JTextField](http://stackoverflow.com/q/26651047/2587435), where a [`DocumentListener`](https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html) and a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) is used – Paul Samsotha Nov 14 '14 at 10:57

1 Answers1

1

If you want to simulate the button press, including in the UI, you can call the following method:

myButton.doClick();

This will act as the user physically pressed it. Otherwise, you can create an ActionEvent and pass it to the button's listeners:

// Does not appear in the UI
for(ActionListener a: mybutton.getActionListeners()) {
    a.actionPerformed(yourActionEvent);
 }

In your case though(considering the code posted) I suggest making a method out of the button's code, and just call it wherever you want:

public void showData(){
    try {
        AddEmployee.add(jTextField1.getText());
        jLabel7.setText(AddEmployee.name);
        jLabel12.setText(AddEmployee.dep);
        jLabel10.setText(AddEmployee.pos);
        jLabel11.setText(AddEmployee.time);
        num++;
        jName.append("\n"+Integer.toString(num)+". "+AddEmployee.name);
        jTime.append("\n"+Integer.toString(num)+". "+AddEmployee.time);
        jTextField1.setText("");
    } 
    catch (ClassNotFoundException | SQLException | ParseException ex) {
        Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
    }  
}

private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {    
    showData();
}

private void myOtherComponentEventOcurred(java.awt.event.ActionEvent evt) {    
    showData();
}
Genos
  • 413
  • 4
  • 12