Knight of Ni
Knight of Ni

Reputation: 1830

How to get events from text changed on editable ComboBox?

I am using editable ComboBox in JavaFX. I connected event handler:

ComboBox cb = new ComboBox ();
cb.getEditor().setOnKeyTyped( ... );

And this works fine if I am typing from keyboard. But what if I want to detect event on text change like this:

cb.getEditor().setText(val);

The event handlers doesn't fire. I couldn't find any way to do that. Do you have any hints?... Thanks.

Upvotes: 0

Views: 1791

Answers (2)

Measveasna Mang
Measveasna Mang

Reputation: 1

private void EditeActionPerformed(java.awt.event.ActionEvent evt) 
{ 
    // TODO add your handling code here:
    String btntitle=this.Edite.getText();
    if(btntitle.compareToIgnoreCase("edite")==0) {
        int index=this.Table.getSelectedRow();
        this.txtProductId.setText(""+tm.getValueAt(index, 0));
        this.txtLastName.setText(""+tm.getValueAt(index, 1));
        this.txtFirstName.setText(""+tm.getValueAt(index, 2));
        this.txtTel.setText(""+tm.getValueAt(index, 3));
        this.jComboBoxGender.setSelectedIndex(tm.getValueAt(index, 4));
        this.Edite.setText("update");
    }

    if(btntitle.compareToIgnoreCase("update")==0) {
       int index=Table.getSelectedRow();
       int productid=Integer.parseInt(txtProductId.getText());
       String lastname=txtLastName.getText();
       String firstname=txtFirstName.getText();
       String sex=txtTel.getText();

       tm.setValueAt(productid,index, 0);
       tm.setValueAt(lastname,index, 1);
       tm.setValueAt(firstname,index, 2);
       tm.setValueAt(sex,index, 3);
       cleardata();
    }
}

Upvotes: 0

Uluk Biy
Uluk Biy

Reputation: 49195

Track the textProperty instead:

cb.getEditor().textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
        System.out.println(oldValue + "  -->  " + newValue);
    }
});

Upvotes: 2

Related Questions