set spacing among elements within button javafx - button

I want to specify spacing between two elements within JavaFX Buttons, Here is the Code :
ImageView fiv = new ImageView(new Image("/modified/map.png"));
fiv.setFitHeight(20);
fiv.setPreserveRatio(true);
Button cr = new Button( "Crop", fiv);
Here I want to Specify Spacing Between "Crop" and fiv, How can i do this ?

Use graphicTextGap property of the button.

Related

Placing buttons on opposite sides of an adjustable window in JavaFX

I have two buttons that I'd like to use in my display screen: a reset button, and a settings button.
The problem is, I want them on opposite sides of the window (reset in the top right corner and settings in the top left) while being the same height. When I expand the window, however, the buttons stay in the same place, but I want them to move with the window walls.
I first tried using setAlignment of my reset button to Pos.*TOP_RIGHT* and the settings button to top left, but it comes out with the settings button being on top of the reset button.
resetButton = new Button("Reset Gameboard");this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
this.setTop(resetbackBox);`
I currently am using (below) which during the initial window appears to be working and correctly spaced, but as soon as you expand the window the reset button doesn't follow it, although the settings button does.
`resetButton = new Button("Reset Gameboard");
this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
resetbackBox.setSpacing(380);
this.setTop(resetbackBox);`
The problem start from this line:
resetbackBox.setSpacing(380);
You have setted a fixed spacing value. To fix that you can easily add a component to HBox called Region, this component allow to set a grow area between two buttons:
Button resetButton = new Button("Reset Gameboard");
Button backButton = new Button("Back to Settings");
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
HBox topHBox = new HBox(backButton, region, resetButton);
this.setTop(resetbackBox);

SetPadding of one single elemet in a layout whith many childrens

There is a way to set a Padding of one single element in a VBox having other childrens?
this.layout_p = new VBox();
this.txta_p = new TextArea();
this.m_p = new Button("m");
this.o_p = new Button("o");
this.c_p = new Button("c");
this.oa_p = new Button("oa");
this.np_p = new Button("np'");
layout_p.getChildren().addAll(txta_p, m_p,
o_p, c_p, oa_p, np_p);
layout_p.setAlignment(Pos.CENTER_RIGHT);
I would like to have the button: np_p with a top padding of 50 from others buttons.
So having a separation between oa_p and np_p.
(Without using another VBox for that button and so, setPadding(...))
Setting a padding for a single node does not make sense. Padding is the room between the bounds of a node and it's content, see also the CSS Box Model.
You're actually trying to add a margin here, i.e. some space around the node. This can be done using VBox.setMargin.
VBox.setMargin(np_p, new Insets(50, 0, 0, 0));

JavaFx Change position or Location of a button manually?

I have a problem with changing the location of my Button i want to set a Specific location where to put it but when i use .setLayoutX and .setLayoutY i even tried using .relocate and it is still not working am i doing something wrong? here is my sample code.
CODE
Button b1 = new Button();
b1.setText("Press");
b1.setLayoutX(100);
b1.setLayoutY(120); // this are just some randome numbers for this sample
StackPane pane = new StackPane();
pane.getChildren().add(b1);
Scene scene = new Scene(pane,500,500);
stage.setScenes(scene);
stage.show();
when i run the program the button is still in the center of the screen am doing something wrong?
Use Anchor pane or a pane to align nodes as per your layout.
StackPane, GridPane and many panes they come with inbuilt layout methods, you dont have control on locating the nodes based on x and y values.

JavaFX buttons with same size

I have these buttons with different size:
Image
How I can make all buttons with same with size?
It depends on layout where the button is located. For example, if you add all the buttons into GridPane or BorderPane, you have to specify each button width to correspond to certain variable. In the following example I wrap all buttons inside VBox, set VBox preference width and tie up all buttons minimum width to it:
VBox vBox = new VBox();
vBox.setPrefWidth(100);
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMinWidth(vBox.getPrefWidth());
btn2.setMinWidth(vBox.getPrefWidth());
vBox.getChildren().addAll(btn1, btn2);
It is also worth to mention that there are two ways to specify the button size. You can do it in the java code or specify it in javafx .fxml file. The above method is an example for java code implementation.
You can also unclamp a button's maximum dimensions so it will grow to fill the available space (unlike most nodes, by default a button node has it's max size clamped to it's preferred size so it doesn't usually grow to fill available space). An Oracle tutorial on Tips for Sizing and Aligning Nodes explains this in more detail.
VBox vBox = new VBox();
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMaxWidth(Double.MAX_VALUE);
btn2.setMaxWidth(Double.MAX_VALUE);
vBox.getChildren().addAll(btn1, btn2);
using css you can override the preferred width of all buttons like
.button {
-fx-pref-width: 200px;
}
or create your own style class for certain button groups and add the style to the button like:
css:
.my-special-button {
-fx-pref-height: 28px;
-fx-pref-width: 200px;
}
and then set the style to your button with either
fxml:
styleClass="my-special-button"
or in java
myButton.getStyleClass().add("my-special-button");

Is different font sizes possible in a button in javafx

final Button btnl2 = new Button("SIGN IN");
btnl2.setFont(Font.font("Calibri", FontWeight.BOLD, 16));
btnl2.setPrefSize(150, 60);
btnl2.setStyle(" -fx-base: #0066cc;");
Image imageOk1 = new Image(getClass().getResourceAsStream("signin.png"));
btnl2.setGraphic(new ImageView(imageOk1));
This is the code of one of the buttons in my project. The .png image is displayed in middle of the button. I want to know 2 things :
1) Is it possible to display it to the left side of the button ?
2) Is it possible to have different font sizes in the button ? Now my button has "SIGN IN" label on it. I want to add a new label to the same button with a different font size below the "SIGN IN" label.
1) yes IIRC it is named ContentDisplay but i could be wrong and it's named differently
2) yes but you don't use the text attribute any more but eg but a mixtutre of hbox/vbox/label/imageview on the graphic attribute who accepts a Node
Button b = new Button();
VBox box = new VBox();
box.getChildren().addAll(new Label("line1"), new Label("line2"));
b.setGraphic(box);

Resources