Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX how to clear TextField when mouse is clicked on it

Tags:

java

javafx

I have a problem with chosing correct action on TextField. On Java class:

@FXML
private TextField projectNameInput;
    
@FXML
private void clearProjectName(MouseEvent event) {

    // some if - else statements
    projectNameInput.clear();
}

On FXML it looks like this:

<TextField fx:id="projectNameInput" onMouseClicked="#clearProjectName" GridPane.columnIndex="1">

I tried most options, in Java I tried (as arg) Event, MouseEvent, ActionEvent, combined with FXML onAction, etc but no luck. What kind of action should I choose that when I click the mouse button on TextField it will be automatically cleared?

like image 445
RichardK Avatar asked Feb 14 '15 14:02

RichardK


People also ask

How do I remove a label in JavaFX?

If you want to erase the content of the label, do: final Label result = ... root. add(result, 0, 1); button.

How do you make a TextField not editable in JavaFX?

In FXML, add editable="false" to your TextField tag. Or uncheck the "Editable" checkbox in Scene Builder.


1 Answers

TextField1.addEventFilter(KeyEvent.KEY_TYPED, numeric_Validation(5));
    
AutoControl.setOnMousePressed(new EventHandler<MouseEvent>() {
    
    @Override
    public void handle(MouseEvent event) {
        TextField1.clear();
    }
}
like image 175
kintu david Avatar answered Sep 28 '22 05:09

kintu david