Bonjour,
J'ai défini une classe javaFX Application : HTMLEditorSAmple.java qui est démarrée à partir d'une classe Test.java. elle affiche un panneau d'édition de Texte.
Lorsque j'ai terminé de frapper mon texte, je termine par Platform.exit() puis je retourne à ma classe appelante .
Cela marche bien, mais si j'ai besoin de taper un nouveau texte, je ne sais pas comment faire car la méthode launch() ne peut être lancé qu'une seule fois.
Dans mon exemple ci-dessous, j'ai créé une boucle pour entrer 3 textes différents. Évidemment cela ne marche pas.
Si vous avez des idées, merci de les partager.
Gérard
Classe Appelante :
Classe Application javaFX
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 package exercices; public class Test { public void ok( ) { int count = 1; for (int i=0; i<3; ) { String args = "Bonjour Gérard. " + count++; javafx.application.Application.launch(HTMLEditorSample.class, args); String res = HTMLEditorSample.htmlText; System.out.println(res); } } public static void main(String[] args) { new Test().ok( ); } }
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 exercices; import java.io.File; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.*; import javafx.scene.control.Button; import javafx.scene.control.ToolBar; import javafx.scene.control.Tooltip; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; public class HTMLEditorSample extends Application { protected boolean saveText = false; public static String htmlText = "Ceci est juste un test"; public static Stage stage; @Override public void start(Stage primaryStage) { stage = primaryStage; primaryStage.setTitle("HTMLEditor Sample"); primaryStage.setMaxWidth(700); primaryStage.setMaxHeight(800); HTMLEditor htmlEditor = new HTMLEditor(); htmlText = getParameters().getUnnamed().get(0); htmlEditor.setPrefHeight(800); htmlEditor.setStyle( "-fx-font: 16 cambria;" + "-fx-border-color: brown; " + "-fx-border-style: dotted;" + "-fx-border-width: 6;" ); ToolBar bar = null; Node node = htmlEditor.lookup(".top-toolbar"); if (node instanceof ToolBar) { bar = (ToolBar) node; // System.out.println( "Size before layout pass: "+bar.getItems().size()); } htmlEditor.setHtmlText(htmlText); VBox vBox = new VBox(htmlEditor); primaryStage.setScene(new Scene(vBox)); primaryStage.show(); //invokes layout pass if (bar == null) return ; // On charge les icones pour la barre de tâche ImageView imgViewS= loadImage("images/save.png"); ImageView imgViewQ= loadImage("images/quit.png"); // on ajoute maintenant une icone à ta barre de tâche Button sauver = new Button(); sauver.setOnAction(event -> saveText=true ); sauver.setTooltip(new Tooltip("Sauvegarder")); Button quitter = new Button(); quitter.setOnAction(event -> { htmlText = htmlEditor.getHtmlText(); // stage.close(); Platform. exit(); ; }); quitter.setTooltip(new Tooltip("Quitter")); quitter.setGraphic(imgViewQ); if (imgViewQ != null) { quitter.setGraphic(imgViewQ);bar.getItems().add(0, quitter) ;} if (imgViewS != null) {sauver.setGraphic(imgViewS); bar.getItems().add(1, sauver) ;} } private ImageView loadImage(String img) { // on ajoute maintenant une icone à ta barre de tâche File f = new File(img); ImageView imgView = new ImageView(); if (f.exists()) { try { Image monImage =new Image(new java.io.FileInputStream(img),32,32,false,false); if (monImage != null) { imgView = new ImageView(); imgView.setImage(monImage); } } catch (Exception ex) { System.out.println("ImageView pas créée"); } finally { return imgView; } } else { System.out.println("fichier non trouvé"); return null; } } public static void main(String[] args) { launch(args); } }
Partager