Using TabPane in JavaFX - java

I'm switching from using a flowpane to a tabpane. I have no idea how to.
I want to use a canvas and a pane within the tabpane is this possible?
Below is my code without all the styling etc.
public View(TabPane root) {
this.root = root;
tab1 = new Tab();
root.getTabs().add(tab1);
tab2 = new Tab();
root.getTabs().add(tab2);
ui = new Pane();
canvas = new Canvas(600,600);
//gc is graphics context
gc = canvas.getGraphicsContext2D();
//this is where the problem is??
//i have no idea how to add ui and canvas to my tab1
tab1.getChildren().addAll(ui, canvas);
}

Tab is not a subclass of Pane, so it has no getChildren() method.
Instead, it has a content property whose value is the node displayed in the tab (note there is only one node in a tab).
So you can display the canvas in the tab with
tab1.setContent(canvas);
and of course if you have two things to display, you would put them in some other container and set the content of the tab to the container:
VBox vbox = new VBox(ui, canvas);
tab1.setContent(vbox);

Related

Adding buttons to a scroll pane

I am currently using Scene Builder and to design my FXML UI in Java. However when loading the scene, I would like to loop through a user's devices and display them as buttons inside a scroll pane:
public DeviceSelectorController() {
for (Device device : OrderBird.getInstance().getProfileManager().getActiveProfile().getDevices()) {
Label label = new Label(device.getName());
Button button = new Button(device.getName());
listView.getItems().add(device.getName());
}
}
I cannot do this through Scene Builder as far as I know because I cannot access the cached HashMap.

How can I add a Node to an HBox inside of a Tab JavaFX?

I have a GUI that I've created, and I would like to add a ProgressIndicatorwhen the application is doing something in the background. I've created a Tab in the constructor similar to the following:
public class myGUI {
Tab myTab;
myGUI() {
myTab = new Tab("My Tab");
HBox view = new HBox();
VBox left = new VBox();
BorderPane right = new BorderPane();
/*A lot of other things are declared that go in left and right*/
view.getChildren().addAll(left, right);
myTab.setContent(view);
}
...
Later on, I have a button press that starts the application doing a background task, and I would like to add a ProgressIndicator to the center of the BorderPane. I tried something like the following:
private void handleMyAction(MouseEvent e) {
myTab.getContent().getChildren().get(1).setCenter(new ProgressIndicator(-1.0f));
}
I would think that this works, however, getContent returns a Node, and I cannot call getChildren on that Node. How can I access the BorderPane to add another Node without making the BorderPane a field in my class?
Just make the border pane an instance variable:
public class MyGUI {
private Tab myTab;
private BorderPane right ;
MyGUI() {
myTab = new Tab("My Tab");
HBox view = new HBox();
VBox left = new VBox();
right = new BorderPane();
/*A lot of other things are declared that go in left and right*/
view.getChildren().addAll(left, right);
myTab.setContent(view);
}
private void handleMyAction(MouseEvent e) {
right.setCenter(new ProgressIndicator(-1.0f));
}
}

Why image disappears from titled pane after mouse event?

I have a titled pane, and I'm displaying an image in this pane, but the image is very big so I resize it, to fit the pane. Since the image becomes smaller, the user might want to view it in original size, so I implemented a method on mouse click opening the original image in new bigger window.
imgViewPicture.setFitWidth(titledPanePicture.getPrefWidth());
imgViewPicture.setPreserveRatio(true);
imgViewPicture.setSmooth(true);
imgViewPicture.setCache(true);
titledPanePicture.setContent(imgViewPicture);
The problem is, when I click on titledPanePicture the small image disappears, its content is cleared. Why? For the solution I added a reloading of the content on mouseclick event. But I dont like it, I think the content should stay the same for titledPanePicture.
Here is my mouseclick event:
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
Group root = new Group();
Scene scene = new Scene(root);
scene.setFill(Color.BLACK);
HBox box = new HBox();
imgViewPicture.setFitWidth(0);
box.getChildren().add(imgViewPicture);
root.getChildren().add(box); // before tPane
stage.setTitle("Picture Original Size");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
reloadContentofTitledPane(); // This line is the fix but I find it unnecessary
Tips would be appreciated.
You seem to use the same ImageView in both scenes. However a Node can only be placed at a single place in a single scene graph.
Create a new node for your new Scene.
ImageView newImageView = new ImageView(imgViewPicture.getImage());
...
box.getChildren().add(newImageView);

JavaFX Scene: Add Scene To Tab

Tab tab1 = new Tab();
Tab tab2 = new Tab();
How do I add a scene to a Tab?
I want to make it so when tab1 is selected the scene is showing and when switched to tab2, it is not there.
I tried doing tab1.setContent, it has to be a node.
I tried doing dialog.setOwner(tab1), it has to be a window.
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("FIRST TAB");
Tab tab2 = new Tab("SECOND TAB");
tab1.setContent(new VBox(new Text("Scene Below:")));
tab2.setContent(new VBox(new Button("dsadsadassda")));
tabPane.getTabs().addAll(tab1, tab2);
final Stage dialog = new Stage();
dialog.initModality(Modality.NONE);
dialog.initOwner(tab1); // I can't, it has to be a window
dialog.initStyle(StageStyle.UTILITY);
dialog.show();
stage.setScene(new Scene(tabPane, 1000, 680));
stage.setMaximized(true);
stage.show();
well the design depends on what you want to do , in your case you should specify a CustomTab that extends the JavaFx Tab and as default it would hold a ScrollablePane or whatever pane you want , which will be bound to have the same width and height as the Tab, so when you do setContent() you should be able to pass every JavaFx's object, because you will add that object to the inner tab pane.

Javafx BorderPane Force Overlap

I have this code (simplified/shortend/pseudo):
StackPane background = new StackPane();
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane(background);
pane.setLeft(overlay);
I want the background/Stack pane to cover the entire window (in the background, surprise), and the overlay/Grid to cover a part of the background. The issue is that when I add an ImageView to the StackPane, the image shows up and nothing else. The overlay is not visible. I have tried overlay.toFront, overlay.preferredHeight, and background.toBack, with no success.
Try putting the border pane into the stack pane, instead of the other way around, and then making sure you add the image as the first element of the stack pane's children.
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane();
pane.setLeft(overlay);
StackPane background = new StackPane(pane);
// ...
ImageView imageView = ... ;
background.getChildren().add(0, imageView);
// ...
scene.setRoot(background);
Alternatively, just use a BorderPane as the root and add the nodes in the usual way. Then, in an external style sheet, do
.root {
-fx-background-image: url(relative/path/to/image);
}

Categories