How to align/move Vaadin buttons - css

I am learning Vaadin and I would like to move two buttons to the bottom of pop up window with spacing in between the buttons. I'm pretty sure I would have to override the button css in my theme but how do I change the absolute location of the button in java code?
Here is my code: A simple button with a click listener that calls a pop up method (subwindow). In the below code I'm trying to move the yes button to the bottom of pop up window.
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
Button helloButton = new Button("Click Me");
helloButton.addClickListener(e -> helloPopUp());
layout.setMargin(true);
layout.setSpacing(true);
layout.addComponents(helloButton);
setContent(layout);
}
private void helloPopUp() {
Window subWindow = new Window("Pop Up");
HorizontalLayout subContent = new HorizontalLayout();
AbsoluteLayout layout2 = new AbsoluteLayout();
subContent.setMargin(true);
Label text = new Label( "Hello Pop Up" , ContentMode.PREFORMATTED);
subContent.addComponent(text);
Button yes = new Button("Yes");
Button no = new Button("No");
layout2.addComponent(yes, "left: 50px; bottom: 0px;");
subContent.addComponents(layout2);
subContent.addComponent(no);
subWindow.setContent(subContent);
UI.getCurrent().addWindow(subWindow);
}

Here is a way to do this without using AbsoluteLayout
private void helloPopUp() {
Window subWindow = new Window("Pop Up");
VerticalLayout subContent = new VerticalLayout();
subContent.setMargin(true);
Label text = new Label( "Hello Pop Up" , ContentMode.PREFORMATTED);
subContent.addComponent(text);
Button yes = new Button("Yes");
Button no = new Button("No");
HorizontalLayout buttonsLayout = new HorizontalLayout();
buttonsLayout.addComponents(yes, no);
buttonsLayout.setSpacing(true);
subContent.addComponent(buttonsLayout);
subContent.setComponentAlignment(buttonsLayout, Alignment.BOTTOM_LEFT);
subWindow.setContent(subContent);
UI.getCurrent().addWindow(subWindow);
}

Related

JavaFx when I open dialog.showAndWait() how to focus TextArea

public Node dialog(){
Pane root = new Pane();
root.setPrefWidth(200);
root.setPrefHeight(200);
Button button = new Button("Dialog");
button.setOnAction(event -> {
Dialog dialog = new Dialog();
TextArea textArea = new TextArea();
dialog.getDialogPane().setContent(textArea);
ButtonType ok = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.getButtonTypes().addAll(ok, cancel);
textArea.requestFocus();
dialog.showAndWait();
});
root.getChildren().add(button);
return root;
}
I try use textArea.requestFocus(); before dialog.showAndWait(); but when dialog open it always
focus OK Button. How to foucs textArea when dialog first open?
This should do the trick:
dialog.setOnShown(event -> {
Platform.runLater(textArea::requestFocus);
event.consume();
});
dialog.showAndWait();

Button over View in JavaFX

Is it possible to make Button over the View?
I have something like
private BorderPane makePane(Node view) {
BorderPane pane = new BorderPane();
Button leftButton = new Button();
Button rightButton = new Button();
Button bottomButton = new Button();
leftButton.setOnAction(e -> goBack(pane));
rightButton.setOnAction(e -> goNext(pane));
bottomButton.setOnAction(e -> play());
pane.setCenter(view);
pane.setLeft(leftButton);
pane.setRight(rightButton);
pane.setBottom(bottomButton);
return pane;
}
Now, I have view on the centter and buttons on the sides. But it crops my Image or Video, depends on which instance of Node I get.
There is crop on the pane from left and right sides for each button. I don't want to see the white/black/other vertical line through whole window only for 1 button.

How do I set a tab for tabpane in Javafx?

I am trying to create a button in my tabpane, that can switch to another tab(0 → 1).
This is for creating a chat program using Javafx, and I'm currently using Scenebuilder to design the GUI.
myTabPane_1.getSelectionModel().select(1);
I expect it to change from tab index # '0' to index # '1', but it stays the same.
There is something that you are not showing in your code and because you did not supply enough information no one will be able to help you modify your code so here is a working example of how to change tabs using index and by passing the actual tab
public class Main extends Application {
#Override
public void start(Stage stage) {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
Tab tabOne = new Tab("Tab One", new Label("This is Tab One"));
Tab tabTwo = new Tab("Tab Two", new Label("This is Tab Two"));
TabPane tabPane = new TabPane();
tabPane.getTabs().addAll(tabOne, tabTwo);
vBox.getChildren().add(tabPane);
Button button = new Button("Change Tab by passing index");
button.setOnAction(event -> {
if(tabPane.getSelectionModel().getSelectedItem().getText().equals("Tab One"))
tabPane.getSelectionModel().select(1);
else
tabPane.getSelectionModel().select(0);
});
vBox.getChildren().add(button);
Button buttonTwo = new Button("Change Tab by passing tab");
buttonTwo.setOnAction(event -> {
if(tabPane.getSelectionModel().getSelectedItem().getText().equals("Tab One"))
tabPane.getSelectionModel().select(tabTwo);
else
tabPane.getSelectionModel().select(tabOne);
});
vBox.getChildren().add(buttonTwo);
stage.setScene(new Scene(vBox));
stage.show();
}
}

Gluon Mobile Toggle Button Jumping

I'm trying to implement a Gluon Mobile toggle button for a survey page, and when testing, the button jumps to the left a little when I click it. I don't want it to jump at all. You can see it here:
Relevant code is here:
StackPane getToggler() {
ToggleButton toggleButton = new ToggleButton("Yes");
ToggleButtonGroup toggleButtonGroup = new ToggleButtonGroup();
toggleButtonGroup.setSelectionType(SelectionMode.SINGLE);
toggleButtonGroup.setPadding(new Insets(10));
toggleButton = new ToggleButton("Yes");
toggleButton.setStyle("-fx-text-fill:steelblue;");
toggleButton.setUserData("1");
toggleButton.setSelected(false);
toggleButton.selectedProperty().addListener((obv, ov, nv) -> {
if (nv.booleanValue()) {
toggleButtonGroup.setUserData("1");
}
});
toggleButtonGroup.getToggles().add(toggleButton);
toggleButton = new ToggleButton("No");
toggleButton.setStyle("-fx-text-fill:steelblue;");
toggleButton.setSelected(true);
toggleButton.setUserData("0");
toggleButton.setSelected(false);
toggleButton.selectedProperty().addListener((obv, ov, nv) -> {
if (nv.booleanValue()) {
toggleButtonGroup.setUserData("0");
}
});
toggleButtonGroup.getToggles().add(toggleButton);
togglers.add(toggleButtonGroup);
StackPane wrapper = new StackPane();
wrapper.setAlignment(Pos.CENTER);
wrapper.getChildren().add(toggleButtonGroup);
return wrapper;
}
Here's where I get the togglers and their relation to the label to the left:
for (int i = 0; i < this.questions.length; i++) {
HBox row = new HBox();
row.setSpacing(5);
row.setAlignment(Pos.CENTER_LEFT);
Label label = new Label(this.questions[i]);
label.setWrapText(true);
label.setPrefWidth(200);
label.setTextAlignment(TextAlignment.LEFT);
label.setFont(new Font("System", 14));
StackPane wrapper = this.getToggler();
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
HBox.setHgrow(label, Priority.NEVER);
row.getChildren().addAll(label,region,wrapper);
box.getChildren().add(row);
box.getChildren().add(new Separator());
}
After some debugging, I've realized that the min width value of the toggle buttons is wider than their pref width.
This means that after the user selects one toggle, the min width is applied, and the control is resized with the required min width, shrinking the region as a consequence.
A quick fix (until this gets fixed in the control) can be setting the min width of your toggle buttons:
private StackPane getToggler() {
ToggleButtonGroup toggleButtonGroup = new ToggleButtonGroup();
ToggleButton toggleButtonYes = new ToggleButton("Yes");
toggleButtonYes.minWidthProperty().bind(toggleButtonYes.prefWidthProperty());
toggleButtonGroup.getToggles().add(toggleButtonYes);
ToggleButton toggleButtonNo = new ToggleButton("No");
toggleButtonNo.minWidthProperty().bind(toggleButtonNo.prefWidthProperty());
toggleButtonGroup.getToggles().add(toggleButtonNo);
...;
}

How to hide full screen button of Mac OS window (in JavaFX)?

Every window title bar of Mac OS has a full screen button at the top-right corner.Is there any way to hide this default full screen button of Mac OS in JavaFX?
Here is my code snippet:
public static void launchOkMessageBox(){
pane = new VBox();
scene = new Scene(pane,150,60, Color.GHOSTWHITE);
Label label = new Label("Hello Word");
Button okButton = new Button("Ok");
pane.getChildren().add(label);
pane.getChildren().add(okButton);
pane.setAlignment(Pos.CENTER);
pane.setSpacing(10);
messageBoxStage.setScene(scene);
messageBoxStage.setResizable(false);
messageBoxStage.sizeToScene();
messageBoxStage.show();
okButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
messageBoxStage.close();
}
});
}
At least for the new dialog API it is sufficient have an owning Window with modality set to APPLICATION_MODAL (default):
Alert alert = new Alert();
alert.initOwner(mainStage);
One way to do it would be to set the StageStyle to StageStyle.UTILITY
messageBoxStage.initStyle(StageStyle.UTILITY);

Resources