2 pièce(s) jointe(s)
Comment forcer le curseur à être devant un suffixe ?
Bonjour,
En JavaFx, j'applique un TextFormatter sur un TextField pour forcer le curseur à être avant un suffixe (je cherche à créer un champ "degré", donc le suffixe est °).
Quand je lance mon application et que je clique sur le TextField, J'ai ce qui suit:
Pièce jointe 617127
Je voudrais le curseur avant °, comme ça:
Pièce jointe 617128
Voici mon code:
HelloApplication.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
} |
HelloController.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
public class HelloController {
@FXML
private TextField input;
@FXML
private void initialize() {
input.setText("°");
input.setTextFormatter(getSuffix("°"));
}
public TextFormatter<String> getSuffix(String suffix) {
return new TextFormatter<>(pChange -> {
int maxPos = pChange.getControlText().length() - suffix.length();
if (pChange.isContentChange()) {
pChange.setRange(Math.min(pChange.getRangeStart(), maxPos), Math.min(pChange.getRangeEnd(), maxPos));
} else {
pChange.setCaretPosition(Math.min(pChange.getCaretPosition(), maxPos));
pChange.setAnchor(Math.min(pChange.getAnchor(), maxPos));
}
return pChange;
});
}
} |
hello-view.fxml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null" fx:controller="com.example.demo.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<children>
<TextField fx:id="input" />
</children>
</VBox> |
Avez-vous une solution ?
Merci