I am trying to scroll a ScrollPane that contains an XYChart via code.
#FXML
private ScrollPane graphSP;
Scrolling it, for instance, to the half-way point works with this sequence:
Stage stage = new Stage();
stage.show();
graphSP.setHvalue(.5);
The problem is, if I place that call to setHvalue() elsewhere, it just does nothing.
So wondering, what are the constraints to actually cause the ScrollPane to scroll? Or, where can I call setHvalue() in my program.
You need to set the Hvalue/Vvalue of the ScrollPane after the scene is visible i.e. stage.isShowing() is true.
EDIT
One way to call it is after the stage.show() is called.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
ScrollPane scrollPane = new ScrollPane(chart);
primaryStage.setScene(new Scene(scrollPane, 300, 300));
primaryStage.show();
scrollPane.setVvalue(0.5);
scrollPane.setHvalue(0.5);
}
public static void main(String[] args) {
launch(args);
}
}
But, there may be cases where a reference to stage is not available. In those case you can use the following :
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
ScrollPane scrollPane = new ScrollPane(chart);
primaryStage.setScene(new Scene(scrollPane, 300, 300));
scrollPane.getScene().getWindow().showingProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
scrollPane.setVvalue(0.5);
scrollPane.setHvalue(0.5);
}
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related
I try to add Swing component to JavaFX application. I create SwingNode object with JButton but don't add this element to main Scene. After closing main stage, application stay running. What I need to do to fix this?
Example here:
public class SwingFx extends Application {
#Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
AnchorPane pane1 = new AnchorPane();
AnchorPane pane2 = new AnchorPane();
pane1.getChildren().add(swingNode);
stage.setScene(new Scene(pane2, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
swingNode.setContent(new JButton("Click me!"));
}
});
}
public static void main(String[] args) {
launch(args);
}
}
I don't know why it happens, but I can give you some dirty solution to fix it. Application stay running after closing if you create SwingNode and don't add it to Scene. So you need to add SwingNode to Scene, but with zero size. This must fix your problem.
Example:
public class SwingFx extends Application {
#Override
public void start(Stage stage) {
// create pane with SwingNode
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
StackPane paneWithSwing = new StackPane(swingNode);
// create invisible pane to initialize SwingPane in Scene
Pane invisiblePane = new Pane(paneWithSwing);
invisiblePane.setPrefWidth(0);
invisiblePane.setPrefHeight(0);
invisiblePane.setOpacity(0);
// create Scene without visible SwingPane
Button button = new Button("Set SwingNode");
button.setOnAction(event -> {
invisiblePane.getChildren().clear();
stage.getScene().setRoot(paneWithSwing);
});
StackPane originalPane = new StackPane(invisiblePane, button);
stage.setScene(new Scene(originalPane, 320, 180));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(() -> swingNode.setContent(new JButton("Click me!")));
}
public static void main(String[] args) {
launch(args);
}
}
JavaFX MenuBar has a large gap between it and any Menus until a resize is performed.
After resize, the Menus sit flush against the MenuBar as expected. Assuming this is not widespread behavior because a search doesn't turn anything up (or I'm searching the wrong terms).
Java: 1.8.0_20
OS: Ubuntu 14.04
Before resize:
After resize:
Any ideas on how to get consistent behaviour here?
Code used to make the screenshots:
public class DemoFX extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
root.setPrefSize(400, 400);
Menu a = new Menu("A");
a.getItems().addAll(new MenuItem("A1"), new MenuItem("A2"));
MenuBar bar = new MenuBar();
bar.getMenus().add(a);
root.setTop(bar);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I am basically new to Java FX 2.
Scenario:
I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.
If This is Possible How Can I do this.
Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html
public class Main extends Application {
final ImageView pic = new ImageView();
final Label name = new Label();
final Label binName = new Label();
final Label description = new Label();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Menu Sample");
Scene scene = new Scene(new VBox(), 400, 350);
scene.setFill(Color.OLDLACE);
MenuBar menuBar = new MenuBar();
// --- Graphical elements
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
vbox.getChildren().addAll(name, binName, pic, description); // name is lable
// --- Menu File
Menu menuFile = new Menu("File");
MenuItem add = new MenuItem("Shuffle",
new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
add.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
shuffle();
vbox.setVisible(true);
}
});
MenuItem clear = new MenuItem("Clear");
clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
clear.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
vbox.setVisible(false);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
stage.setScene(scene);
stage.show();
}
}
So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??
The approach I would prefer is to use a Scene with BorderPane as its root
scene.setRoot(borderPane);
You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);
Whenever you need to switch to WebView just replace it with SplitPane :
borderPane.setCenter(webView);
Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView
I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
Then within createProduct.fxml, when menuItem is onclick, it will perform this:
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
It does opened up createCategory.fxml. However, the previous panel which is createProduct.fxml does not close. I know there's something called stage.close() to do this but I have no idea where to implement it since I not passing the scene from main right from the start. I wonder how should I fix this.
Thanks in advance.
You have to make some changes in start method, like..
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createProduct.fxml"));
Pane myPane = (Pane)myLoader.load();
CreateProductController controller = (CreateProductController) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
and your CreateProductController.java will be,
public class CreateProductController implements Initializable {
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
}
}
You have multiple ways to solve your problem, few of them I tell you. What I understood from your question is that you want to create an application which contains a navigation (menu bar) on the top and by using it user can switch to any screen.
The easiest method is to make your Navigation Bar available globally, either by you make it static or by passing it to screens, or by any other method. Second, make a single screen (say Border Pane) and load other screens on it (its center).
You can also use spring integration in project so that it provide you better injection of controllers. See Ref : http://www.zenjava.com/2011/10/25/views-within-views-controllers-within-controllers/
You can also create your own single screen controller and manage other screens using that.
See Ref: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1
But for this (above) you have to change or update your current architecture.
you can get the current open window/stage from the fx:id . just make sure you have an item in your createProduct.fxml that has fx:id like below
<TextField fx:id="username" labelFloat="true" layoutX="80.0" layoutY="300.0" prefHeight="32.0" prefWidth="260.0" promptText="Login Username" />
Hope you see the fx:id attribute.
Then in your start controller, leave as it is
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("createProduct.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
Then in your CreateProductController.java
//make sure you import the javafx item you have used, eg have used TextField
import javafx.scene.control.TextField;
public class CreateProductController implements Initializable {
// make sure the variable has the same type as the item in the fxml "TextField" and same name "username"
#FXML
private TextField username;
//we use the above variable since its referencing the window to which it belongs because of the fx:id attribute, then we close it
private void closeStage()
{
((Stage) username.getScene().getWindow()).close();
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
closeStage();// we close the old stage
Stage stage = new Stage();
stage.setTitle("Shop Management");
Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
stage.show();
}
}
Since updating to JavaFX 2.0 b36 (SDK for Windows (32Bit) + Netbeans Plugin) from a previous JavaFX 2.0 version the SplitPane control does not work as expected any longer.
The divider can't be moved
The divider position is not as expected
The sizing of the contained sides is not as expected
Here my example code for a SplitPane .
public class FxTest extends Application {
public static void main(String[] args) {
Application.launch(FxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 200, 200, Color.WHITE);
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
SplitPane splitPane = new SplitPane();
splitPane.setPrefSize(200, 200);
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.setDividerPosition(0, 0.7);
splitPane.getItems().addAll(button1, button2);
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}
As you can (hopefully) see the left side is clearly smaller than the right side.
Another funny fact is, when you change orientation to VERTICAL
splitPane.setOrientation(Orientation.VERTICAL);
and try to move the divider up or down you get some console output saying 'HERE'.
Looks like some test output.
What's the issue with this?
To get the SplitPane working as expected add a layout (e.g. BorderPane) to each side. Add the controls to display to each of these layouts. I think this should be made more clear in API documentation!
public class FxTest extends Application {
public static void main(String[] args) {
Application.launch(FxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 200, 200, Color.WHITE);
//CREATE THE SPLITPANE
SplitPane splitPane = new SplitPane();
splitPane.setPrefSize(200, 200);
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.setDividerPosition(0, 0.7);
//ADD LAYOUTS AND ASSIGN CONTAINED CONTROLS
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
BorderPane leftPane = new BorderPane();
leftPane.getChildren().add(button1);
BorderPane rightPane = new BorderPane();
rightPane.getChildren().add(button2);
splitPane.getItems().addAll(leftPane, rightPane);
//ADD SPLITPANE TO ROOT
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}