I want to wrap text in a new line in some controls.
For example when there is not enough space to display the whole text of a checkbox, there should be a new line for that. I don't want these dots ("...").
The same behaviour I would like in a TableCell (not editable) and in a TextField or TextArea.
I tested .setWrapText(true) but this didn't help...
Can anybody help me please?
Best regards
It looks like it depends on the layout you are using.
For example this code wraps the CheckBox text no problem:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class CheckBoxTest extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 300, 200);
stage.setScene(scene);
CheckBox checkBox = new CheckBox("This is a really really really really really really really really long string");
checkBox.setWrapText(true);
pane.setCenter(checkBox);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
But if I were to change the BorderPane to a FlowPane, the wrapping would not work.
Related
I have not worked much with gui before, but I am trying to learn it by building a javafx application using SceneBuilder. Now I have trouble getting ImageView to resize when I resize the window.
I am using a gridPane, and i have placed a ImageView on a anchorPane inside one of the routes in the gridPane. The gridPane is changing size when I change the size of my window when i run the application, but the ImageView does not change size. I want the Imageview to change size correspondingly when i change the size of the window when i run the application.
have tried to read similar issues here on the forum, but have not found a solution that I can use, Does anyone have a good way to do this?
Really appreciate all answers.
You can use binding for simplicity, here the full example code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage){
GridPane gridPane = new GridPane();
AnchorPane anchorPane = new AnchorPane();
// Set image with url
Image image = new Image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR-524e2cCaY9TUn8vglN3nqqOlT0jIJIIty-Z81S49q9Np6Yws");
ImageView imageView = new ImageView(image);
// Bind the imageView width property to gridPane width property
// So, if width of gridPane change, the width of imageView automatically will be change
imageView.fitWidthProperty().bind(gridPane.widthProperty());
// Make the ratio same with original image
imageView.setPreserveRatio(true);
anchorPane.getChildren().add(imageView);
gridPane.getChildren().add(anchorPane);
Scene scene = new Scene(gridPane, 600, 400);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I believe it is quite simple, though I couldn't figure it out yet:
How do I change the color of the placeholder text in JavaFX TableView?
This is the placeholder text, which is shown if the table is empty:
There are (at least) two ways to solve this.
Via Css
Consult the JavaFx Css Reference and there you will see, that a TableView has an internal placeholder, that you can address using Css.
If documentation is insufficient or you need to more information about the structure of the Scenegraph, use ScenicView to explore.
Via placeholder node
Consulting the JavaFx 8 Api documentation will reveal, that there is a placeholder property, that allows you to set a custom node as placeholder.
Demo
This demo showcases both approaches:
TableViewPlaceholderFill.java:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class TableViewPlaceholderFill extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
TableView<String> tableViaCss = new TableView<>();
tableViaCss.getStyleClass().add("my-little-pony");
TableView<String> tableWithCustomPlaceholder = new TableView<>();
final Label placeholderLabel = new Label
("Hello, Kitty!");
placeholderLabel.setFont(Font.font("monospace", FontWeight.BLACK, 16));
placeholderLabel.setTextFill(Color.HOTPINK);
tableWithCustomPlaceholder.setPlaceholder(new StackPane(placeholderLabel));
Scene scene = new Scene(new HBox(4,tableViaCss,
tableWithCustomPlaceholder));
scene.getStylesheets().add(TableViewPlaceholderFill.class
.getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
application.css:
.my-little-pony {
-fx-background-color: palevioletred;
}
.my-little-pony .placeholder .label {
-fx-text-fill: linen;
-fx-font-family: 'serif';
-fx-font-size: 1.666em;
-fx-font-weight: bold;
}
i've been searching for hours and could not find any way to make the tableView columns and tabs of a TabbedPane start from the right side instead of left.
As we can see in the picture java by default create them from the left side to the right, and leave empty space on the right side. Is there any way to do this vice versa ?
Thank you
see image here
Use setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class NodeOrientationTest extends Application {
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
Tab tab1 = new Tab("Tab 1");
Tab tab2 = new Tab("Tab 2");
tabPane.getTabs().addAll(tab1, tab2);
TableView<Void> table = new TableView<>();
table.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
table.getColumns().add(new TableColumn<Void, Void>("Column 1"));
table.getColumns().add(new TableColumn<Void, Void>("Column 2"));
tab1.setContent(table);
Scene scene = new Scene(tabPane, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Note that the table view will, by default, inherit this from its parent, so you can omit the call to table.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT); and achieve the same result. (Or just set it on the scene.)
My goal is to make it so that when a user slides a Slider, an object that is like a straw bends from its middle.
Sort of like this:
http://s.hswstatic.com/gif/bendy-straw-a-1.jpg
I've spent days searching for ways to model this with JavaFX shapes, but maybe I'm missing something obvious. When the Slider is at zero, the shape should be a straight line, whereas when the Slider is bent all the way, the shape should resemble an arm of a hyperbola.
Thank you so much.
You'll have to figure out the math correctly, but something like this is what I had in mind. You can even try just leaving the control point Y in the middle of the line, I moved it up since I figured it's more realistic, like it will start to bend at the bottom. You also have to do some calculus to figure out the end point. The length shouldn't change.
package curve;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.scene.shape.QuadCurve;
import javafx.stage.Stage;
public class Curve extends Application {
#Override
public void start(Stage primaryStage) {
QuadCurve qc = new QuadCurve(100, 500, 100, 250, 100, 0);
qc.setStroke(Color.BLUE);
qc.setStrokeWidth(5);
qc.setFill(Color.TRANSPARENT);
Slider slider = new Slider(0, 200, 0);
qc.endXProperty().bind(slider.valueProperty().add(100));
qc.endYProperty().bind(slider.valueProperty().add(100).divide(1.2));
qc.controlYProperty().bind(slider.valueProperty().multiply(-1).add(500));
Group group = new Group(qc,slider);
Scene scene = new Scene(group, 500,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
How can I display a new window in JavaFX 2.0? For example after button click action.
I want both windows (the main window and the new window) to communicate each other.
Thx for help.
new Stage(new Scene(new Group(new Text(10,10, "my second window")))).show();
Communicating between two windows is similar as for any two objects in Java.
You create new windows by calling new Stage() and show them by stage.show().
Here is an example of creating a new Stage with a checkbox control which modifies text of a label displayed in a different Stage.
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
public class SecondStage extends Application {
public static void main(String[] args) { launch(args); }
#Override public void start(Stage primaryStage) {
// setup some dymamic data to display.
final String STANDARD_TEXT = "Every Good Boy Deserves Fruit";
final String ALTERNATE_TEXT = "Good Boys Deserve Fruit Always";
final Label label = new Label(STANDARD_TEXT);
// configure the primary stage.
StackPane primaryLayout = new StackPane();
primaryLayout.getChildren().add(label);
primaryLayout.setStyle("-fx-background-color: lightgreen; -fx-padding: 10;");
primaryStage.setScene(new Scene(primaryLayout, 200, 100));
primaryStage.setTitle("Primary Stage");
// configure the secondary stage.
final Stage secondaryStage = new Stage(StageStyle.UTILITY);
CheckBox alternateTextCheck = new CheckBox("Show alternate text");
alternateTextCheck.selectedProperty().addListener(new ChangeListener<Boolean>() {
#Override public void changed(ObservableValue<? extends Boolean> selected, Boolean oldValue, Boolean newValue) {
if (newValue) label.setText(ALTERNATE_TEXT); else label.setText(STANDARD_TEXT);
}
});
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(alternateTextCheck);
secondaryLayout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
secondaryStage.setScene(new Scene(secondaryLayout, 200, 100));
secondaryStage.setTitle("Secondary Stage");
// specify stage locations.
secondaryStage.setX(400); secondaryStage.setY(200);
primaryStage.setX(400); primaryStage.setY(350);
// add a trigger to hide the secondary stage when the primary stage is hidden.
// this will cause all stages to be hidden (which will cause the app to terminate).
primaryStage.setOnHidden(new EventHandler<WindowEvent>() {
#Override public void handle(WindowEvent onClosing) {
secondaryStage.hide();
}
});
// show both stages.
primaryStage.show();
secondaryStage.show();
}
}
Inside the button click action you can create a new satge and then a object of the other class you want to display. after that call the start method using the created object.
Stage stage= new Stage();
NewClass nc= new NewClass();
nc.start(stage);
hope this will work!!!