I have TextArea in JavaFX. I need to align text vertically or make TextArea to be same size as content. Currently, TextArea is growing to fill its parent height.
I sorted it by creating a Label, placing it behind TextArea, making it invisible(just in case) and binding TextArea height to label:
#FXML
private TextArea msg;
#FXML
private Label bindingText;
#FXML
private Button button;
#FXML
private void initialize() {
bindingText.textProperty().bind(msg.textProperty());
msg.setText(Word.customTranslate("data_transfer_failed", Locale.getDefault().getLanguage()));
msg.prefHeightProperty().bind(bindingText.heightProperty());
button.setText(Word.customTranslate("close_app", Locale.getDefault().getLanguage()));
}
Get rid of TextArea borders through styling and center it. This way I got centered text, that can also be selected.
Related
I have a GridPane (4x5), all it's cells have as child an AnchorPane which cointains an ImageView. I need to resize the image so it fully cover the cell as soon as the gridPane (and thus it's cells) change size.
I managed to resize the image correctly when the size of the cell grows, but when the cell gets tinier the image doesn't resize back.
This leads into partially covering images of the confinant cells.
Can anyone explain what i'm doing wrong or give me the instruction to implement a proper resize?
This is my code:
ImageView image = new ImageView("/dice/" + draftList.get(i) + ".png");
AnchorPane pane = ((AnchorPane)(gridpane.getChildren().get(i)));
pane.getChildren().add(image);
fitToParent(image,pane);
//method in the same class
private void fitToParent(ImageView image, AnchorPane pane) {
image.fitWidthProperty().bind(pane.widthProperty());
image.fitHeightProperty().bind(pane.heightProperty());
}
You can try to use the setPreserveRatio(boolean) function of the ImageView class to true. This will allow you to keep the aspect ratio constant.
Eg:
ImageView iv = new ImageView(/*file path*/);
iv.setPreserveRatio(true);
Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html
Other than this you can also try to limit the resizable property to false or set the min width and height so that the image is not partially covered
Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#resize-double-double-
As soon as I create a new Button in JavaFX and set the background transparent with: myButton.setBackground(Background.EMPTY); or myButton.setStyle("-fx-background-color: transparent;"),
the hitbox will only consist of the text in the button when catching the ActionEvent via :
myButton.setOnAction(newjavafx.event.EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//handle UI input
}
});
So I have to aim on a letter and click it, which is annoying, especially when having changing text and/ or small text.
How can I keep my hitbox the same while having a transparent background?
Use
myButton.setPickOnBounds(true);
which means that the layout bounds of the button will be used to determine mouse hits on it, rather than its set of non-transparent pixels.
I have a requirement to display a Rectangle with an InnerShadow effect as the background of my JavaFX nodes. Each node needs to be resizable. I am implementing this with an abstract base class with a Region which has the Rectangle with an InnerShadow as one of its children and a Region provided by the concrete implementation of this class as its other child.
The problem occurs when I add one or more ComboBoxes to the child region. Clicking on the ComboBoxes has no effect, i.e. they do not display their drop-down list of items.
I have tried making the Rectangle smaller (i.e. 10x10 pixels) so that it does not overlap the ComboBoxes. This makes no difference.
private void createNodeWithBackground() {
pane = new Region() {
#Override
public void resize(double width, double height) {
super.resize(width, height);
backing = new Rectangle(width, height);
InnerShadow shadeEffect = new InnerShadow();
shadeEffect.setWidth(w/2);
shadeEffect.setHeight(h/2);
shadeEffect.setInput(new ColorAdjust(-0.1, 0.2, -0.1, 0.1));
backing.setEffect(shadeEffect);
getChildren().clear();
getChildren().addAll(backing, getBodyNode());
}
};
getChildren().add(pane);
}
/**
* The concrete class provides a node to be displayed on top of the
* Rectangle with the InnerShadow.
* This might be a VBox containing a ComboBox and other nodes.
*/
protected abstract Region getBodyNode();
Removing the Rectangle from the scene results in the ComboBoxes displaying drop-down lists as expected.
Using javafx.scene.control.ChoiceBox instead of javafx.scene.control.ComboBox resolves this issue, although I have no idea why.
I'm trying to resize the button text when the window change size, but i don't know how to do that. I'm using NetBeans, JavaFX and Scene Builder.
Look at the images:
The button grows larger but the font size remains the same.
You can use setFont method.
For example
button.setFont(Font.font(40));
to change text style
button.setFont(Font.font("Calibri",FontWeight.BOLD,20));
You can change the scaleX property and scaleY property.
Assuming you are resizing with an outerPane.
final double origHeight = outerPane.getPrefHeight();
final double origWidth = outerPane.getPrefWidth();
button.scaleXProperty().bind(outerPane.widthProperty().divide(origWidth));
button.scaleYProperty().bind(outerPane.heightProperty().divide(origHeight));
You could paste the code different places depending on how you setup your application.
I tested by pasting this into FXMLDocumentController, replacing the empty initialze() that was already there:
#FXML
private Button button;
#FXML
private AnchorPane outerPane;
#Override
public void initialize(URL url, ResourceBundle rb) {
final double origHeight = outerPane.getPrefHeight();
final double origWidth = outerPane.getPrefWidth();
button.scaleXProperty().bind(outerPane.widthProperty().divide(origWidth));
button.scaleYProperty().bind(outerPane.heightProperty().divide(origHeight));
}
Save and compile, then reopen SceneBuilder and set the fx:id in the Code tab on the right for the pane that gets resized to outerPane and the Button to button.
You should bind a value to Font Size and apply it using styleProperty(), like this:
DoubleProperty fontSize22 = new SimpleDoubleProperty(22);
fontSize22.bind(homeButton.widthProperty().add(homeButton.heightProperty()).divide(10));
homeButton.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize22.asString(), "px;"));
I have a variety of ToggleButtons that are loaded with an image. The buttons size is determinated by the image size and the button itself is created by JavaCode. A few buttons have icons (the icon is just part of the loaded image) on either the left or right side.
How can I move the text by a certain value to the left or right so I can center the text again but with the offset of the icon? I don't mind passing the width of the icon as parameter, but I cant find anything to move the text for a certain amount.
The button is created from the green image, the right icon is part of it; total width is 300, icon is taking 100; the text should be centered to the leftover 200. For language setting reason the text itself cant be part of the picture.
you can set the style of the button as followed:
// top right bottom left
btn.setStyle("-fx-padding: 5px 5px 5px 5px;");
EDIT:
You can use an HBox:
HBox hbox = new HBox();
// the text of the "button"
Label lbl_txt = new Label("Text");
// the icon of the "button", i am using Labels for icon(with css)
Label lbl_ico = new Label("ico");
hbox.getChildren().addAll(lbl_txt, lbl_ico);
hbox.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
// alternative to Button click
}
});
Everything else is styling with css. ;-)