JavaFX labels height - java

I'm developing software in JavaFX and JavaFX Scene Builder.
I have a grid pane with 2 columns. Actually in each of the cells there are labels.
In the first column of the table the text inside the label is a default constant, in the second one the text can change.
The problem is that if the text variable is too long, it's automatically truncated.
How to start a new line trimming the text and re-sizing the height of the rows in the grid pane?
EDIT:
Thanks to #fabian issues, this is the solution to the initial question.
In addiction is necessary to set the fx:id of each element into Scene Builder.
#FXML private GridPane gridPane;
#FXML private Text text1;
#FXML private Text text2;
#FXML private Text text3;
private void setGridRowHeight(GridPane gpName, int numRow, double height){
ObservableList<RowConstraints> rows = gpName.getRowConstraints();
rows.get(numRow).setMinHeight(32);
rows.get(numRow).setPrefHeight(height);
rows.get(numRow).setMaxHeight(height);
}
private void addTextListener(final GridPane gridPane, final Text text){
text.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
scrollPane.setFitToHeight(true);
text.setWrappingWidth( gridPane.getColumnConstraints().get(1).getPrefWidth() );
setGridRowHeight(gridPane, gridPane.getRowIndex(text), text.getLayoutBounds().getHeight() );
}
});
}
#FXML public void initialize() {
addTextListener(gridPane, text1);
addTextListener(gridPane, text2);
addTextListener(gridPane, text3);
}

Unfortunately the Label element does not resize depending on the text displayed. I recommend using the Text Shape. You can specify the maximal width using wrapping width in the scene builder.

You could try:
Label col2Label = new Label();
col2Label.setWrapText( true );
col2Label.setMaxHeight( Double.MAX_VALUE );
col_1_Label.prefHeightProperty().bind( col2Label.heightProperty() );

You can wrap [Region - Label - Region] in HBox.
The region will keep the label always centered.

Related

How to display white text with a black background?

I am trying to figure out why my text isn't being displayed here. I assume it isn't being colored white, and simply doesn't show up against the black background. So, I'm trying to figure out why the background is being colored black, but the text isn't being shown.
I am using JavaFX 12.
How can I color the letters white?
public class MCVE extends Application {
private static Stage gameStage = new Stage();
#Override
public void start(Stage primaryStage) {
gameStage.setScene(new Scene(createScreen(), 1280.0, 800.0));
// adjust window settings.
gameStage.setTitle("my game");
gameStage.setResizable(false);
gameStage.show();
}
private Pane createScreen() {
Text welcome = new Text("Welcome,");
welcome.setFont(new Font("Old_Style", 22.0));
welcome.setStyle("-fx-text-fill: whitesmoke");
Text toMyGame = new Text(" to my game.");
toMyGame.setFont(new Font("System", 14.0));
toMyGame.setStyle("-fx-text-fill: whitesmoke");
TextFlow tf = createTextFlow();
ObservableList list = tf.getChildren();
list.addAll(welcome, toMyGame);
return tf;
}
private TextFlow createTextFlow() {
String style = "-fx-background-color: black;";
style += " -fx-text-fill: whitesmoke;";
TextFlow tf = new TextFlow();
tf.setStyle(style);
return tf;
}
}
-fx-text-fill is a CSS property of Labeled, not Text. Text is a type of shape, and Shape supports -fx-fill rather than -fx-text-fill.
Changing every occurrence of -fx-text-fill to -fx-fill should give you the appearance you want.

Multilne text. Libgdx

How to crate a multiline text? I tried to use Label , but text have only 1 line.
Table table = new Table();
table.setPosition(0,0);
table.setSize(800,440);
label = new Label("Some long string here...", skin);
label.setFillParent(true);
label.setAlignment(center);
label.setPosition(0,0);
label.setWidth(40);
label.setHeight(label.getPrefHeight());
label.setText(tutortext);
table.addActor(label);
stage.addActor(table);
stage.setDebugAll(true);
https://i.stack.imgur.com/3whQr.png
Use setWrap(true); on label.
According to doc.
If false, the text will only wrap where it contains newlines (\n). The preferred size of the label will be the text bounds.
If true, the text will word wrap using the width of the label. The preferred width of the label will be 0, it is expected that something external will set the width of the label. Wrapping will not occur when ellipsis is enabled. Default is false.
When wrap is enabled, the label's preferred height depends on the width of the label. In some cases the parent of the label will need to layout twice: once to set the width of the label and a second time to adjust to the label's new preferred height.
I've tested with small example :
public class MultiLine extends ApplicationAdapter {
Stage stage;
Skin skin;
#Override
public void create() {
stage=new Stage();
skin=new Skin(Gdx.files.internal("skin/glassy-ui.json"));
Label label=new Label("MULTILINE IN LIBGDX GAME DEVELOPMENT",skin);
label.setWrap(true);
Table table=new Table();
table.setFillParent(true);
table.add(label).width(150);
stage.addActor(table);
}
#Override
public void render() {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
Here is the output :
A way simpler solution is to just use \n for linebreaks so instead of
Label label=new Label("MULTILINE IN LIBGDX GAME DEVELOPMENT",skin);
you would just do
Label label=new Label("MULTILINE\nIN\nLIBGDX\nGAME\nDEVELOPMENT",skin);
The label will be:
MULTILINE
IN
LIBGDX
GAME
DEVELOPMENT

Resizing individual Text Objects

Over the last week or 2, I've been experimenting with different ways to manipulate shape and text objects in a scene using JavaFX.
Currently, I'm looking at creating new text objects and being able to manipulate their size.
So I have a scene, with a button that simply says "Text", that once pressed
creates a TextField whereby upon entering text into this field creates a new text object. There is also a slider which you're able to use to change the size of the text object. and you're able to drag the various Text objects about
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CreatingNewText extends Application
{
private int size;
private TextField enterText;
private Slider sizeSlider;
private Button button;
public static void main(String [] args)
{
launch(args);
}
public void start(Stage primaryStage)
{
Pane root = new Pane();
// Button for creating new text Object
button = new Button("text");
button.setLayoutX(200);
// Slider for Size
sizeSlider = new Slider(0,255,0);
sizeSlider.setLayoutX(250);
sizeSlider.setLayoutY(0);
// Button functionality
button.setOnAction(e ->{
Text text = new Text(150,300,"Text");
// Moving created text
text.setOnMouseDragged(f ->{
text.setX(f.getX());
text.setY(f.getY());
});
text.setLayoutX(300);
text.setLayoutY(300);
text.setFont(Font.font("Phosphate"));
// Text entry field
enterText = new TextField();
text.textProperty().bind(enterText.textProperty());
// Slider functionality
text.setOnMouseEntered(g ->{
});
sizeSlider.valueProperty().addListener((ObservableValue
<? extends Number> ov, Number curVal, Number newVal) -> {
size = (int) sizeSlider.getValue();
Font fontSize = Font.font(size);
text.setFont(fontSize);
});
root.getChildren().addAll(enterText,text);
});
root.getChildren().addAll(button,sizeSlider);
Scene scene = new Scene(root,600,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The issues -
I cannot change the size of a specific text object - I understand that every time I create a new text object it assigns it to the "text" variable, and so upon altering the size with the slider all Text shapes associated with "text" will change also, how can this be corrected?
I am also sure the same thing is happening for the text field - every time the text button is pushed it just creates a new textfield which just sits on top of the old one. I have tried creating my textfield outside of the action event - similar to the slider, but cannot because the text object is local to the action event. Making "text" an instance variable just complicates things and I receive an "exception in application start method" error - So basically it doesn't work. Really what I want is just the one text field that can create multiple text objects
Apologies about the code. I am very inexperienced and it even looks a mess to me. Hope the question makes sense too
Look at this sample app made from your code. I think it demonstrates what you are trying to achieve :
Click on a text it become Red and you can adjust it's size with your slider
public class CreatingNewText extends Application {
private int size;
private TextField enterText;
private Slider sizeSlider;
private Button button;
private Text selected;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Pane root = new Pane();
// Button for creating new text Object
button = new Button("text");
button.setLayoutX(200);
// Slider for Size
sizeSlider = new Slider(0, 255, 0);
sizeSlider.setLayoutX(250);
sizeSlider.setLayoutY(0);
// TextField
enterText = new TextField();
// Button functionality
button.setOnAction(e -> {
Text text = new Text(150, 300, "Text");
// Moving created text
text.setOnMouseDragged(f -> {
text.setX(f.getX());
text.setY(f.getY());
});
text.setLayoutX(300);
text.setLayoutY(300);
text.setFont(Font.font("Phosphate"));
text.textProperty().bind(enterText.textProperty());
text.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
if (selected != null) {
selected.setFill(Color.BLACK);
}
selected = (Text) mouseEvent.getTarget();
selected.setFill(Color.RED);
});
root.getChildren().addAll(text);
});
sizeSlider.valueProperty().addListener((ObservableValue<? extends Number> ov, Number curVal, Number newVal) -> {
if (selected != null) {
size = (int) sizeSlider.getValue();
Font fontSize = Font.font(size);
selected.setFont(fontSize);
}
});
root.getChildren().addAll(button, sizeSlider, enterText);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFx, Not to display content at the beginning

I am writing a javafx program, for moving text. The program display the background (some red points) and text at the beginning.
Now, I would like to add a menu to select the content of text to display. In addition, I want the scene to display nothing at the beginning, then after I select content of text, the program starts to display everything...
Is there any special command to implement this?
PS: I add the contents (text and shapes) -> "Group" -> "root".
I think what you want is something along this:
public class PersonOverviewController implements Initializable{
#FXML
BorderPane paneWithControls;
#FXML
MenuItem menuItem;
#FXML
Label text;
#FXML
private void initialize() {
paneWithControls.setVisible(false); //hide content
menuItem.setOnAction(new EventHandler<ActionEvent>() { //implementing action listener
#Override
public void handle(ActionEvent event) {
text.setText("Some text you want to display"); //set value for controls
paneWithControls.setVisible(true); //display content
}
});
}
}
EDIT:
If you really want to "hide" the scene you can do something like this:
primaryStage.setScene(null);
and later on:
primaryStage.setScene(sceneObject1);
However this is not good way to achieve result you are looking for. Once you have the scene, just change the root of it, or set properties of it's controls. I have mistaken this also when I was learning JavaFX.

Get a child node to be displayed inside an Anchor Pane in Java FX?

Just for testing I want a Button which is a Node so far to be inside a TitledPane (I know it contains an AnchorPane).
This is the general code I'm trying for the method:
#FXML private void alterarTitledPane(MouseEvent event) {
if (event.isShiftDown()) {
// Here is where I code for adding a button inside TitledPane
}
if (!titledPane.isExpanded()) {
titledPane.setExpanded(true);
titledPane.setText("Expandido");
}
else {
titledPane.setExpanded(false);
titledPane.setText("No expandido");
}
}
These are the two alternatives I've tried for achieving it:
Alternative 1
((AnchorPane)titledPane.getContent()).getChildren().add(button2);
button2.toFront();
button2.setVisible(true);
Alternative 2
Group a = new Group(button2);
((AnchorPane)titledPane.getContent()).getChildren().add(a);
button2.toFront();
button2.setVisible(true);
Got not results so far...
for add node in pane first of all you have to initalize it...
try this....its work.
AnchorPane anch = new AnchorPane();
Button btn = new Button("anshul");
anch.getChildren().add(btn);
Anchorpane pane=new Anchorpane();
Label lbl_user=new Label("Username");
Textfield txt=new Textfield();
AnchorPane.setTopAnchor(lbl_user, 10.0);
AnchorPane.setRightAnchor(lbl_user 10.0);
AnchorPane.setTopAnchor(txt, 40.0);
AnchorPane.setRightAnchor(txt,10.0);
panegetChildren().addAll(lbl_user,txt);

Categories