Last active
October 13, 2021 23:30
-
-
Save aoetk/7d5cc13e64239d1233e6dd879fed682e to your computer and use it in GitHub Desktop.
Java 9のデスクトップAPIをJavaFXから利用するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.test.desktop; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class DesktopAPITestApp extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("DesktopAPITestView.fxml")); | |
primaryStage.setTitle("Desktop API Test"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.geometry.Insets?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.ListView?> | |
<?import javafx.scene.control.ProgressBar?> | |
<?import javafx.scene.layout.HBox?> | |
<?import javafx.scene.layout.VBox?> | |
<VBox xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="aoetk.test.desktop.DesktopAPITestViewController"> | |
<children> | |
<ListView fx:id="displayList" maxWidth="1.7976931348623157E308" prefHeight="200.0" prefWidth="400.0" VBox.vgrow="ALWAYS"> | |
<VBox.margin> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</VBox.margin> | |
</ListView> | |
<HBox alignment="CENTER_LEFT" spacing="10.0"> | |
<children> | |
<ProgressBar fx:id="progress" maxWidth="1.7976931348623157E308" progress="0.0" HBox.hgrow="ALWAYS" /> | |
<Button fx:id="startButton" mnemonicParsing="false" text="Start" HBox.hgrow="NEVER" /> | |
</children> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</HBox> | |
</children> | |
</VBox> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.test.desktop; | |
import javafx.animation.AnimationTimer; | |
import javafx.application.Platform; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.*; | |
import javafx.scene.control.Button; | |
import java.awt.*; | |
import java.awt.desktop.*; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class DesktopAPITestViewController implements Initializable { | |
private static final long PROGRESS_TIME = 5_000_000_000L; | |
@FXML | |
Button startButton; | |
@FXML | |
ProgressBar progress; | |
@FXML | |
ListView<String> displayList; | |
private ObservableList<String> eventList = FXCollections.observableArrayList(); | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
displayList.setItems(eventList); | |
if (Desktop.isDesktopSupported()) { | |
Desktop desktop = Desktop.getDesktop(); | |
addAppEvents(desktop); | |
setSystemMenuHandler(desktop); | |
} else { | |
System.out.println("デスクトップはサポートされていません."); | |
} | |
if (Taskbar.isTaskbarSupported()) { | |
Taskbar taskbar = Taskbar.getTaskbar(); | |
if (taskbar.isSupported(Taskbar.Feature.ICON_BADGE_NUMBER)) { | |
taskbar.setIconBadge("10"); | |
} else { | |
System.out.println("タスクバーのアイコンバッジへの数値登録はサポートされていません."); | |
} | |
addAction(taskbar); | |
} else { | |
System.out.println("タスクバーはサポートされていません."); | |
} | |
} | |
private void addAction(Taskbar taskbar) { | |
startButton.setOnAction(actionEvent -> { | |
startButton.setDisable(true); | |
AnimationTimer timer = new AnimationTimer() { | |
private long startTime; | |
@Override | |
public void handle(long currentTime) { | |
long elapsedTime = currentTime - startTime; | |
if (elapsedTime > PROGRESS_TIME) { | |
stop(); | |
progress.setProgress(1.0); | |
startButton.setDisable(false); | |
} else { | |
double rateForProgressBar = Long.valueOf(elapsedTime).doubleValue() / PROGRESS_TIME; | |
progress.setProgress(rateForProgressBar); | |
int rateForTaskBar = (int) (rateForProgressBar * 100); | |
if (taskbar.isSupported(Taskbar.Feature.PROGRESS_VALUE)) { | |
taskbar.setProgressValue(rateForTaskBar); | |
} | |
} | |
} | |
@Override | |
public void start() { | |
startTime = System.nanoTime(); | |
progress.setProgress(0); | |
if (taskbar.isSupported(Taskbar.Feature.PROGRESS_VALUE)) { | |
taskbar.setProgressValue(0); | |
} else { | |
System.out.println("タスクバーのプログレス表示はサポートされていません."); | |
} | |
super.start(); | |
} | |
}; | |
timer.start(); | |
}); | |
} | |
private void setSystemMenuHandler(Desktop desktop) { | |
if (desktop.isSupported(Desktop.Action.APP_ABOUT)) { | |
// 現時点ではJavaFXでは何も起きない | |
desktop.setAboutHandler(aboutEvent -> { | |
Alert alert = new Alert(Alert.AlertType.INFORMATION); | |
alert.setContentText("オリジナルのAboutダイアログです."); | |
alert.setHeaderText("設定"); | |
alert.show(); | |
}); | |
} else { | |
System.out.println("Aboutメニューはサポートされていません."); | |
} | |
if (desktop.isSupported(Desktop.Action.APP_PREFERENCES)) { | |
/* 現時点ではJavaFXからは使えない! | |
desktop.setPreferencesHandler(preferencesEvent -> { | |
Alert alert = new Alert(Alert.AlertType.INFORMATION); | |
alert.setContentText("オリジナルの設定ダイアログです."); | |
alert.setHeaderText("設定"); | |
alert.show(); | |
}); | |
*/ | |
} else { | |
System.out.println("設定メニューはサポートされていません."); | |
} | |
} | |
private void addAppEvents(Desktop desktop) { | |
if (desktop.isSupported(Desktop.Action.APP_EVENT_FOREGROUND)) { | |
desktop.addAppEventListener(new AppForegroundListener() { | |
@Override | |
public void appRaisedToForeground(AppForegroundEvent appForegroundEvent) { | |
addMessage("フォアグラウンドになりました.\n"); | |
} | |
@Override | |
public void appMovedToBackground(AppForegroundEvent appForegroundEvent) { | |
addMessage("バックグラウンドになりました.\n"); | |
} | |
}); | |
} else { | |
System.out.println("AppForegroundEventはサポートされていません."); | |
} | |
if (desktop.isSupported(Desktop.Action.APP_EVENT_HIDDEN)) { | |
desktop.addAppEventListener(new AppHiddenListener() { | |
@Override | |
public void appHidden(AppHiddenEvent appHiddenEvent) { | |
addMessage("アプリケーションが隠されました.\n"); | |
} | |
@Override | |
public void appUnhidden(AppHiddenEvent appHiddenEvent) { | |
addMessage("アプリケーションが見えるようになりました.\n"); | |
} | |
}); | |
} else { | |
System.out.println("AppHiddenEventはサポートされていません."); | |
} | |
if (desktop.isSupported(Desktop.Action.APP_EVENT_SCREEN_SLEEP)) { | |
desktop.addAppEventListener(new ScreenSleepListener() { | |
@Override | |
public void screenAboutToSleep(ScreenSleepEvent screenSleepEvent) { | |
addMessage("画面がスリープしようとしています."); | |
} | |
@Override | |
public void screenAwoke(ScreenSleepEvent screenSleepEvent) { | |
addMessage("画面がスリープから復帰しました."); | |
} | |
}); | |
} else { | |
System.out.println("ScreenSleepEventはサポートされていません."); | |
} | |
if (desktop.isSupported(Desktop.Action.APP_EVENT_SYSTEM_SLEEP)) { | |
desktop.addAppEventListener(new SystemSleepListener() { | |
@Override | |
public void systemAboutToSleep(SystemSleepEvent systemSleepEvent) { | |
addMessage("システムがスリープしようとしています."); | |
} | |
@Override | |
public void systemAwoke(SystemSleepEvent systemSleepEvent) { | |
addMessage("システムがスリープから復帰しました."); | |
} | |
}); | |
} else { | |
System.out.println("SystemSleepEventはサポートされていません."); | |
} | |
if (desktop.isSupported(Desktop.Action.APP_EVENT_USER_SESSION)) { | |
desktop.addAppEventListener(new UserSessionListener() { | |
@Override | |
public void userSessionDeactivated(UserSessionEvent userSessionEvent) { | |
addMessage("ユーザセッションが切れました."); | |
} | |
@Override | |
public void userSessionActivated(UserSessionEvent userSessionEvent) { | |
addMessage("ユーザセッションがアクティベートされました."); | |
} | |
}); | |
} else { | |
System.out.println("UserSessionEventはサポートされていません."); | |
} | |
} | |
private void addMessage(String msg) { | |
Platform.runLater(() -> eventList.add(msg)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment