[JavaFX] stage.hide() puis stage.show()
J'ai un problème d'affichage!
J'ai mis mon application JavaFX en mode IconTray avec 3 menu possible (Show, Hide et Close)
- Show devrait afficher mon application
- Hide devrait le masquer
- Close devrait le fermet
Le problème se trouve sur le scénario suivant :
1 - Je clic sur Hide, mon application est vraiment masquée!
2 - Je clic sur Show, rien ne se passe , alors que ma fenètre devrait se réaffichée
Voic mon code
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 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
package gedsynchrone;
import com.ged.scene.AuthentificationController;
import java.awt.AWTException;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
*
* @author nathan
*/
public class GedSynchrone extends Application {
private Parent root;
private TrayIcon trayIcon;
private boolean firstTime;
//
// static SynchroneThread st = new SynchroneThread("Synchronisation");
@Override
public void start(final Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(AuthentificationController.class.getResource("/com/ged/scene/test.fxml"));
root = (Parent) loader.load();
Scene scene = new Scene(root, 300, 250, Color.WHITE);
firstTime = true;
createTrayIcon(primaryStage);
primaryStage.setScene(scene);
primaryStage.getIcons().add(new Image("/com/ged/scene/images/icone.jpg"));
primaryStage.setTitle("GED Desktop");
primaryStage.setMaxWidth(300.0);
primaryStage.setMinWidth(300.0);
primaryStage.setWidth(300.0);
primaryStage.setResizable(false);
Rectangle2D primScreenBounds = Screen.getPrimary().getBounds();
primaryStage.setX((primScreenBounds.getWidth() - 305));
primaryStage.setY((primScreenBounds.getHeight() - 330));
primaryStage.show();
} catch (Exception e) {
}
}
public void createTrayIcon(final Stage stage) {
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
java.awt.Image image = Toolkit.getDefaultToolkit().getImage(AuthentificationController.class.getResource("/com/ged/scene/images/jpg.png"));
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
hide(stage);
}
});
final ActionListener closeListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
};
ActionListener showListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.show();
}
});
}
};
ActionListener hideListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.hide();
}
});
}
};
// create a popup menu
PopupMenu popup = new PopupMenu();
java.awt.MenuItem showItem = new java.awt.MenuItem("Show");
showItem.addActionListener(showListener);
popup.add(showItem);
java.awt.MenuItem hideItem = new java.awt.MenuItem("Hide");
hideItem.addActionListener(hideListener);
popup.add(hideItem);
java.awt.MenuItem closeItem = new java.awt.MenuItem("Close");
closeItem.addActionListener(closeListener);
popup.add(closeItem);
/// ... add other items
// construct a TrayIcon
trayIcon = new TrayIcon(image, "GED Desktop", popup);
// set the TrayIcon properties
trayIcon.addActionListener(showListener);
// ...
// add the tray image
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println(e);
}
// ...
}
}
public void showProgramIsMinimizedMsg() {
if (firstTime) {
trayIcon.displayMessage("Some message.",
"Some other message.",
TrayIcon.MessageType.INFO);
firstTime = false;
}
}
private void hide(final Stage stage) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (SystemTray.isSupported()) {
stage.hide();
// showProgramIsMinimizedMsg();
} else {
System.exit(0);
}
}
});
}
public static void main(String[] args) {
launch(args);
}
} |
Le problème (je crois) c'est que l'aaplication ne retrouve plus le Thread nommé "FX application Thread" et reste bloqué sur le Thread de java.awt après que je clic sur Hide.
Merci de votre aide!