エレミヤBinegar:
私は、日付ピッカークラスに似ていますが、代わりに時間を選択するための方法でポップアップを表示するJavaFXのクラスを作成しました。私は、ポップアップは、TextFieldと日付ピッカーポップアップと同じようにボタンの下に表示されていたいと思っていますが、私は、ノードのXとY座標を取得する方法を見つけることができません。任意の助けいただければ幸いです。
jewelsea:
あなたは使用して画面座標を取得することができます:node.localToScreen(x,y)
。
次のコードは、トップポップアップ所有者ウィンドウのノードの底部中央にポップアップウィンドウの隅を残した(アンカー)を探します。
Point2D anchorPoint = node.localToScreen(
node.getWidth() / 2,
node.getHeight()
);
popup.setAnchorLocation(
PopupWindow.AnchorLocation.WINDOW_TOP_LEFT
);
popup.show(
node,
anchorPoint.getX(),
anchorPoint.getY()
);
サンプルアプリケーション:
表示と非表示のボタンを制御するの下部中央に位置するポップアップ。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
public class Popcorn extends Application {
@Override
public void start(Stage stage) {
StackPane popupLayout = new StackPane(
new Label(
"Clarke's third law\nAny sufficiently advanced technology is indistinguishable from magic."
)
);
popupLayout.setStyle("-fx-opacity: 0.8; -fx-background-color: paleturquoise;");
popupLayout.setPadding(new Insets(10));
Popup popup = new Popup();
popup.getContent().add(
popupLayout
);
popupLayout.setOnMouseClicked(event -> popup.hide());
ToggleButton showPopupButton = new ToggleButton("Show popup");
showPopupButton.textProperty().bind(
Bindings.when(showPopupButton.selectedProperty())
.then("Hide popup")
.otherwise("Show popup")
);
showPopupButton.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
if (isSelected) {
showPopup(popup, showPopupButton);
} else {
popup.hide();
}
});
popup.setOnShown(event -> showPopupButton.setSelected(true));
popup.setOnHidden(event -> showPopupButton.setSelected(false));
StackPane stageLayout = new StackPane(showPopupButton);
stageLayout.setPadding(new Insets(10));
stage.setScene(new Scene(stageLayout));
stage.show();
}
private void showPopup(Popup popup, Control ownerNode) {
Point2D anchorPoint = ownerNode.localToScreen(
ownerNode.getWidth() / 2,
ownerNode.getHeight()
);
popup.setAnchorLocation(
PopupWindow.AnchorLocation.WINDOW_TOP_LEFT
);
popup.show(
ownerNode,
anchorPoint.getX(),
anchorPoint.getY()
);
}
public static void main(String[] args) {
launch(args);
}
}
ポップアップアンカー上の注意事項
ポップアップアンカーポイントは、ポップアップを配置する場所のポップアップシステムにだけアドバイスです。JavaFXのに組み込まれたポップアップ・ロジックは、ポップアップウィンドウのサイズと画面上の最初の場所を知っているスマート十分です。十分な不動産がないことをその手段が提案されたアンカーポイントでポップアップを表示するには、画面上に残っている場合、ポップアップの実装は、自動的に最初に表示されるときのポップアップが完全に見えるようになることを保証するために、アンカーポイントを調整します。あなたは、サンプルコードを実行すると、ポップアップを表示しようとする前に、または右画面の極端なの下部にある所有者のステージを配置することで、これを試してみることができます。