Bonjour,
J'ai essayé de reprendre un exemple du livre JavaFX8 Introduction by Example (à la page 119) ou sur GitHub à l'adresse suivante :
https://github.com/Apress/javafx-8-i...dProcesses.zip
ça marche bien .
Ensuite j'ai voulu le transformer pour créer un MVC avec une partie FXML et la les problèmes arrivent :
Mes boutons ne sont plus gérés correctement et génèrent des erreurs :
Voici mon Controleur :"SampleControler"
Mon XML :Sample.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 package application; import java.net.URL; import java.util.Random; import java.util.ResourceBundle; import javafx.beans.value.ObservableValue; import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.ProgressBar; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.TextArea; public class SampleController { static Task copyWorker; final int numFiles = 30; @FXML private URL location; @FXML private ResourceBundle resources; @FXML private Button Start; @FXML private Button Cancel; @FXML private TextArea textArea; @FXML private ProgressBar progressBar; @FXML private ProgressIndicator progressIndicator; public SampleController() { } @FXML private void initialize() { } @FXML private void Startbutton() { Start.setDisable(true); progressBar.setProgress(0); progressIndicator.setProgress(0); textArea.setText(""); Cancel.setDisable(false); copyWorker = createWorker(numFiles); // wire up progress bar progressBar.progressProperty().unbind(); progressBar.progressProperty().bind(copyWorker.progressProperty()); progressIndicator.progressProperty().unbind(); progressIndicator.progressProperty().bind(copyWorker.progressProperty()); // append to text area box copyWorker.messageProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> { textArea.appendText(newValue + "\n");}); new Thread(copyWorker).start(); } @FXML private void Cancelbutton() { Start.setDisable(false); Cancel.setDisable(true); copyWorker.cancel(true); // reset progressBar.progressProperty().unbind(); progressBar.setProgress(0); progressIndicator.progressProperty().unbind(); progressIndicator.setProgress(0); textArea.appendText("File transfer was cancelled."); } private Task createWorker(final int numFiles) { return new Task() { @Override protected Object call() throws Exception { for (int i = 0; i < numFiles; i++) { long elapsedTime = System.currentTimeMillis(); copyFile("some file", "some dest file"); elapsedTime = System.currentTimeMillis() - elapsedTime; String status = elapsedTime + " milliseconds"; // queue up status updateMessage(status); updateProgress(i + 1, numFiles); // (progress, max) } return true; } }; } private void copyFile(String src, String dest) throws InterruptedException { // simulate a long time Random rnd = new Random(System.currentTimeMillis()); long millis = rnd.nextInt(1000); Thread.sleep(millis); } }
et mon affichage javafx: "Main"
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 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.ProgressBar?> <?import javafx.scene.control.ProgressIndicator?> <?import javafx.scene.control.TextArea?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.Pane?> <BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController"> <bottom> <Pane prefHeight="200.0" prefWidth="1072.0" BorderPane.alignment="CENTER"> <children> <Button id="Start" layoutX="59.0" layoutY="59.0" mnemonicParsing="false" onAction="#Startbutton" prefHeight="25.0" prefWidth="52.0" text="Start" /> <Button id="Cancel" layoutX="59.0" layoutY="123.0" mnemonicParsing="false" onAction="#Cancelbutton" text="Cancel" /> <ProgressBar id="progressBar" layoutX="223.0" layoutY="63.0" prefWidth="200.0" progress="0.0" /> <TextArea id="textArea" layoutX="223.0" layoutY="100.0" prefHeight="65.0" prefWidth="200.0" /> <ProgressIndicator id="progressIndicator" layoutX="458.0" layoutY="43.0" progress="0.0" /> </children> </Pane> </bottom> </BorderPane>
En attente de vos réponses et de vous lire
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 package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.fxml.FXMLLoader; public class Main extends Application { @Override public void start(Stage primaryStage) { try { BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml")); Scene scene = new Scene(root,800,400); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }
Bien Cdt
Partager