Padding button and rectangle - java

My button is located under the rectangle. It is currently like in the picture on the left. I want to pad the button like in the picture on the right.
How can I do that? Thanks for reading.
Here's my code.
public class Rect extends Application {
private Button btn1 = new Button("Rotate");
private Rectangle r1 = new Rectangle(50, 100);
#Override
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = new GridPane();
r1.setStroke(Color.BLACK);
r1.setFill(Color.WHITE);
gridPane.add(r1, 0, 0);
gridPane.add(btn1, 0, 1);
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
Scene scene = new Scene(gridPane,200,200);
primaryStage.setTitle("RotateRectangleFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

At the moment you are doing this:
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
You should be setting the alignment of r1 instead like follows:
r1.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);

Related

Creating two Button Tables in JavaFX

What i would like is to create two arrays of Buttons like this picture.
What i have done so far :
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
Scene scene = new Scene(grid2, 1000, 1000);
grid.setPadding(new Insets(10, 10, 10, 10));
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
grid.add(new Button(), i,j);
}
public static void main(String[] args) {
launch(args);
}
}
This creates the first desired array starting from the top left corner.However i can't seem to be able to place the second one.Adding a button in a location like (40,40) puts it in a different place.How could this be done?

How to make the Button appear on the right side of a TitledPane JavaFX

I have managed to place a Button inside a TitledPane using setGraphic but I want it to be located on the right side; here is the code:
#Override
public void start(Stage primaryStage) {
Accordion acordion = new Accordion();
TitledPane tp1 = new TitledPane();
Button b1 = new Button();
b1.setText("X");
tp1.setGraphic(b1);
acordion.getPanes().addAll(tp1);
Scene scene = new Scene(acordion, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
The Button should be located on the right but I do not know how to do it
Set the alignment property of TitledPane to CENTER_RIGHT:
tp1.setAlignment(Pos.CENTER_RIGHT);

Java FX component that appears from top and goes down

Does exists a Java FX component like one that appears from top and goes down, as in the gif below?
I was able to implement a demo based on transition of pane. It looks like:
The weakness of this approach is it doesn't work with a Window which has standard border, buttons, etc. A developer is responsible to build a nice window based on a Pane.
The other problem is resizing of the parent window will resize the sub pane as well. But if you have not resizable parent dialog it should not be a problem.
You are welcome to copy-paste this code and play with it:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final SubPane subPane = new SubPane();
final TranslateTransition dialogAnimator =
new TranslateTransition(new Duration(500), subPane);
final Button showSubPaneButton = new Button("Slide a sub pane");
showSubPaneButton.setOnAction(event -> {
dialogAnimator.setToY(10);
dialogAnimator.play();
});
subPane.setCloseAction(event -> {
dialogAnimator.setToY(-subPane.getHeight());
dialogAnimator.play();
});
final Pane rootPane = new StackPane(showSubPaneButton, subPane);
rootPane.setPrefWidth(400);
rootPane.setPrefHeight(300);
// By default hide the given pane by moving to the top.
Platform.runLater(() -> subPane.setTranslateY(-subPane.getHeight()));
primaryStage.setTitle("Drop slided dialog");
primaryStage.setScene(new Scene(rootPane));
primaryStage.show();
}
}
class SubPane extends BorderPane {
private final Button dialogButton = new Button("Close");
SubPane() {
final Pane centralPane = new StackPane(dialogButton);
centralPane.setStyle(
"-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);-fx-padding: 10;" +
"-fx-background-color: firebrick;-fx-background-radius: 10;");
setCenter(centralPane);
final Pane blankLeftPane = new StackPane();
blankLeftPane.setPrefWidth(100);
setLeft(blankLeftPane);
final Pane blankRightPane = new StackPane();
blankRightPane.setPrefWidth(100);
setRight(blankRightPane);
final Pane blankBottomPane = new StackPane();
blankBottomPane.setPrefHeight(200);
setBottom(blankBottomPane);
}
void setCloseAction(final EventHandler<ActionEvent> closeCallback) {
dialogButton.setOnAction(closeCallback);
}
}
If you're talking about the dialog window, ControlsFX have something similar NotificationPane.

How to play the TranslateTransition continuously with no stop

I have a Text that contains a String of 7 lines. I want to put this Text in a panel width 500 and height 70 This text should smoothly move slowly from the bottom up and play again and again without stopping.
I did the following code.
But the text only once playing and floating movement is done with small jumps rather smoothly.
How I can do smooth motion and play it again and again without ever stop?
public class TextSh extends Application {
#Override
public void start(Stage primaryStage) {
String message =" Here is the text of seven lines";
Text textRef = TextBuilder.create()
.layoutY(100)
.textOrigin(VPos.TOP)
.textAlignment(TextAlignment.CENTER)
.wrappingWidth(400)
.text(message)
/*.fill(Color.rgb(187, 195, 107))*/
.fill(Color.OLIVE)
.font(Font.font("SansSerif", FontWeight.BOLD, 18))
.build();
TranslateTransition transTransition = TranslateTransitionBuilder.create()
.duration(new Duration(75000))
.node(textRef)
.toY(-1640)
.interpolator(Interpolator.LINEAR)
.cycleCount(Timeline.INDEFINITE)
.autoReverse(true)
.build();
StackPane root = new StackPane();
root.getChildren().add(textRef);
StackPane r = new StackPane();
r.getChildren().add(root);
Scene scene = new Scene(r, 500, 70);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
transTransition.play();
}
public static void main(String[] args) {
launch(args);
}
}

How to resize an Rectangle accordingly with a TextField?

I want to resize an Rectangle shape from JavaFX accordingly with a TextField which automatically resize on Window resize using Anchors.
I tried add anchors to the rectangle also but it won't resize from the width I set from SceneBuilder.
So short story: when my TextField resizes because my Window resize the Rectangle should resize to the same width as the TextField. Thanks
You can bind the rectangle's width to the text field's:
myRectangle.widthProperty().bind(myTextField.widthProperty());
Example:
public class Demo extends Application {
#Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root);
stage.setScene(scene);
TextField myTextField = new TextField("default");
Rectangle myRectangle = new Rectangle();
myRectangle.setHeight(30);
myRectangle.setFill(Color.AQUA);
myRectangle.widthProperty().bind(myTextField.widthProperty());
final VBox hb = new VBox(10);
hb.setPadding(new Insets(5));
hb.getChildren().addAll(myTextField, myRectangle);
scene.setRoot(hb);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Categories