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:
Je voudrais le curseur avant °, comme ça:
Voici mon code:
HelloApplication.java:
HelloController.java:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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(); } }
hello-view.fxml:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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; }); } }
Avez-vous une solution ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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>
Merci
Partager