Cannot Maximize Window with JavaFX - java

I made a custom Minimize button this way:
public MinimizeButton() {
Button button = new Button("-");
button.getStyleClass().clear();
button.getStyleClass().add("actionbutton");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
stage.setIconified(true);
}
});
this.getChildren().add(button);
}
And I obviously called
primaryStage.initStyle(StageStyle.UNDECORATED);
The button is working well.
The issue is that when I try to maximize the Window once the Stage is iconified, it takes a couple of seconds for the Window to redraw the Stage.
Any ideas on how to make the "Maximizing process" of the Window faster?

Fixed it by using
primaryStage.initStyle(StageStyle.TRANSPARENT);
instead of
primaryStage.initStyle(StageStyle.UNDECORATED);

Related

JavaFX BorderPane inside an AnchorPane?

I'm currently trying to show a BorderPane inside my AnchorPane (that is also inside my StackPane). I tried to show a new Scene but it show me 2 items in my Windows taskbar, and this is not really what I want.
For exemple, when you open the Preference in Eclipse, u get another window that is not visible in the taskbar.
So basically, the only thing I need is a window that looks like an ImageView, like anchored in the AnchorPane , not resizable and undecorated.
Expected result at end
This is what I tried:
btnDice.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent arg0) {
Scene newScene = new Scene(new BorderPane(), 230, 100);
Stage newWindow = new Stage();
newWindow.setScene(newScene);
newWindow.show();
}
});

JavaFX Window always on top of Java Swing JFrame - Wrong Behaviour

I have a Java Swing application. For one of javax.swing.JFrame, I use JFXPanel to embed JavaFX content. In JFXPanel, there is ListView (JFrame => JFXPanel => ListView). Double click on ListView's item, will open new JavaFX browser Window, to load HTML content of it's URL.
Assuming 3 windows:
Window-A: Main application window (Swing).
Window-B: JFrame with JFXPanel (contain's ListView).
Window-C: JavaFX browser window
Once Window-C is open, it always stay on top of any other Windows (Window-A, Window-B, & even Netbean IDE Window). When click on Window-A / Window-B, the Window is actived but always stay behind Window-C. The only way to have complete view on Window-A / Window-B, is to move Window-C away so not to overlap with them.
I tried setting these for Window-C, but with no effect:
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
stage.initModality(Modality.NONE);
The intended behaviour is, whenever Window-A / Window-B / Window-C is clicked, it should stay on top, all the other windows should go behind it.
Any idea how to make this work? Is it a mistake to invoke pure JavaFX Window (Window-C) from Swing application?
Below is my code snippet:
public class StockNews extends JFrame {
private final JFXPanel jfxPanel = new JFXPanel();
Scene scene;
VBox vbox;
java.util.List<FeedItem> messages;
ListView<FeedItem> newsListView;
ObservableList<FeedItem> messages_o;
public StockNews(java.util.List<FeedItem> messages) {
super();
this.messages = messages;
initComponents();
}
private void initComponents() {
Platform.runLater(new Runnable() {
#Override
public void run() {
vbox = new VBox();
scene = new Scene(vbox, 500, 500);
jfxPanel.setScene(scene);
messages_o = FXCollections.observableArrayList (messages);
newsListView = new ListView<>(messages_o);
newsListView.setCellFactory(new Callback<ListView<FeedItem>,
ListCell<FeedItem>>() {
#Override
public ListCell<FeedItem> call(ListView<FeedItem> list) {
// draw single cell
return new DisplaySingleNews();
}
}
);
// Double-clicked ListView's item, will open new JavaFX browser Window, loading HTML content of it's URL
newsListView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() > 1) {
FeedItem msg = newsListView.getSelectionModel().getSelectedItem();
URL link = msg.getLink();
Stage stage = new Stage(StageStyle.UTILITY);
// This window always stay on top, even not active. below 2 lines have no effect
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
stage.initModality(Modality.NONE);
WebView browser = new WebView();
stage.setScene(new Scene(browser));
stage.show();
WebEngine webEngine = browser.getEngine();
webEngine.load(link.toString());
}
}
});
vbox.getChildren().addAll(newsListView);
}
});
this.add(jfxPanel, BorderLayout.CENTER);
this.setVisible(true);
}
}

How can i lock a screen in a certain position (javafx)?

i have to make a game for a school project and i was wondering if it's possible to lock a screen in a certain position on the screen. I have a settings button that opens a new stage and i want to make that scene immovable is this possible?
This is the code where i display the settings screen:
Public void buttonAction(){
btnSettings.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
GridPane settingsGrid = new GridPane();
Scene scene1 = new Scene(settingsGrid,375,600);
Stage settingsStage = new Stage();
settingsGrid.setHgap(10);
settingsGrid.setVgap(10);
//settingsGrid.setGridLinesVisible(true);
settingsStage.setTitle("Settings");
settingsStage.setResizable(false);
settingsStage.initStyle(StageStyle.UTILITY);
settingsStage.initModality(Modality.APPLICATION_MODAL);
settingsStage.setScene(scene1);
settingsStage.showAndWait();
}
});
}
Thanks in advance!
you could try this:
settingsStage.initStyle(StageStyle.TRANSPARENT);
However, you could only have one initStyle if I remember correctly. So you have to settingsStage.initStyle(StageStyle.UTILITY); in order to use the above code. What it does is it make the background transparent and the user could not change the position.

Closing a modal window by clicking button

I have a window with a button. Clicking this button opens a modal window.
Now, I want to close this second window by clicking a button, but I can't figure out how.
public class StartMenu extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Go");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
AnotherWindow aw = new AnotherWindow ();
aw.start(stage);
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
 
 
public class AnotherWindow extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
//Code to close window
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
I found the following post by Krzysztof Sz. that helped me find the solution.
public class AnotherWindow extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
((Button)t.getTarget()).getScene().getWindow().hide();
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
 
It is the following piece of code that let's me close the current (modal) window when the button is clicked:
((Button)t.getTarget()).getScene().getWindow().hide();
You want to close a modal window from a click on a different window? If a modal window is visible, how will you get back to the other window?
You might want to use one window: when a button is clicked, hide all the controls in that window and make visible the information you wanted to have in your modal window, along with a button to click. When that button is clicked, reset the window to its' original state.
This just becomes an exercise in showing/hiding controls in a container.

How to set window size on runtime?

I'm developing a JavaFX FXML application. I need to resize the window from the controller class at runtime.
I found out that its possible to do this from the application class by setting maxHeight and maxWidth properties of the stage. But how to do it from the controller class while the application is running?
Define a button in the controller class and set on action of it like
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// OR, if you define btn as #FXML private Button btn.
Stage stage = (Stage) btn.getScene().getWindow();
// these two of them return the same stage
stage.setWidth(new_val);
stage.setHeight(new_val);
}
});
where stage is your primary (main) stage.

Categories