How to change size of ImageView when changing size of window in javafx gui - user-interface

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);
}
}

Related

Change a circle to a square to fill a square in JavaFX

I found an amazing animation in which there is transition from circle/ellipse to square and just before the end of the animation, the square protrudes out a little and then adjusts itself it back to the correct size.
Source: https://clementmihailescu.github.io/Pathfinding-Visualizer/#
I tried to recreate it but I am not able to get the transition effect from a drop of ink to square.
package sample;
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Rectangle originalRectangle = new Rectangle(100,100);
originalRectangle.setFill(Color.WHITE);
originalRectangle.setStroke(Color.BLUE);
Rectangle substituteRectangle = new Rectangle(25,25,50,50);
substituteRectangle.setOpacity(0.0);
substituteRectangle.setFill(Color.TURQUOISE);
substituteRectangle.setStroke(Color.TURQUOISE);
Rectangle yellowRectangle = new Rectangle(100,100);
yellowRectangle.setFill(Color.YELLOW);
root.getChildren().addAll(originalRectangle,substituteRectangle,yellowRectangle);
FadeTransition fadeOutTransition = new FadeTransition(Duration.millis(3000),yellowRectangle); //Make the duration as 1ms to get the instant
fadeOutTransition.setFromValue(1.0);
fadeOutTransition.setToValue(0.0);
FadeTransition fadeInTransition = new FadeTransition(Duration.millis(1),substituteRectangle);
fadeInTransition.setFromValue(0.0);
fadeInTransition.setToValue(0.8);
//To make the square protrute out a bit
ScaleTransition scaleTransition = new ScaleTransition(Duration.seconds(4),substituteRectangle);
scaleTransition.setToX(2.1);
scaleTransition.setToY(2.1);
ParallelTransition parallelTransition = new ParallelTransition(scaleTransition,fadeInTransition);
fadeOutTransition.play();
fadeOutTransition.setOnFinished(e -> {
parallelTransition.setOnFinished(event -> {
root.getChildren().removeAll(substituteRectangle,yellowRectangle);
originalRectangle.setOpacity(1.0);
originalRectangle.setFill(Color.RED);
});
parallelTransition.play();
});
Scene scene = new Scene(root,800,600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
The probably simplest way of achieving this kind of animation would be to simply animate the radius of the circle and constrain it's size using a Rectangle as clip. Also often Timeline provides a animation that is well suited to replace multiple transitions. In this case the fact that it allows for animations between colors as well as animations for a DoubleProperty makes this a good choice for simplifying the animations:
#Override
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Rectangle bounds = new Rectangle(50, 50, 100, 100);
Circle circle = new Circle(bounds.getX() + 0.5 * bounds.getWidth(), bounds.getY() + 0.5 * bounds.getHeight(),
0);
circle.setClip(bounds);
root.getChildren().add(circle);
Timeline animation = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(circle.fillProperty(), Color.BLUE),
new KeyValue(circle.radiusProperty(), 0d)),
new KeyFrame(Duration.seconds(5), new KeyValue(circle.fillProperty(), Color.RED), new KeyValue(
circle.radiusProperty(), 0.5 * Math.hypot(bounds.getWidth(), bounds.getHeight())))); // at the end of the animation the circle should reach the corners -> diameter = diagonale of rect
root.setOnMouseClicked(evt -> animation.playFromStart());
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

TableView columns position and TabbedPane tabs start from Right Side instead of left in JavaFX?

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.)

Javafx GUI programming

So im trying to make 3 buttons on the top panel and 3 radio buttons on the bottom panel, but when i run it, it comes out all weird, would love it if someone can help me. Im still pretty new to GUI so my code might be completely wrong.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
public class ColorFactory extends Application {
#Override
public void start(Stage stage)
{
BorderPane pane = new BorderPane();
// sets the width and height
stage.setHeight(300);
stage.setWidth(500);
//calls the mainpanel constructor
pane.setCenter(new MainPanel());
//make the mainpanel visible using the setVisible(true)
//call the stage.setScene
stage.setScene(new Scene(pane));
// set title to Color Factory
stage.setTitle("Color Factory");
//call stage.show
stage.show();
}
private class MainPanel extends BorderPane
{
public MainPanel()
{
HBox Tpanel = new HBox(25);
Tpanel = new HBox(25);
Tpanel.setPrefWidth(500);
Tpanel.setPrefHeight(50);
Tpanel.setAlignment(Pos.TOP_CENTER);
Button red = new Button("Red");
red.setStyle("-fx-background-color: red");
Button yellow = new Button("Yellow");
yellow.setStyle("-fx-background-color: yellow;");
Button orange = new Button("Orange");
orange.setStyle("-fx-background-color: orange;");
Tpanel.setStyle("-fx-background-color: white;");
Tpanel.getChildren().addAll(red,yellow,orange);
HBox Bpanel = new HBox(15);
Bpanel.setPrefWidth(500);
Bpanel.setPrefHeight(75);
RadioButton green = new RadioButton("Green");
RadioButton blue = new RadioButton("Blue");
RadioButton cyan = new RadioButton("Cyan");
green.setStyle("-fx-background-color: green;");
blue.setStyle("-fx-background-color: blue;");
cyan.setStyle("-fx-background-color: cyan;");
Bpanel.setAlignment(Pos.BOTTOM_CENTER);
Bpanel.getChildren().addAll(green,blue,cyan);
Label label = new Label("Top buttons change the panel color and bottom radio buttons change the text color");
label.setAlignment(Pos.CENTER_LEFT);
label.setTextFill(Color.BLUE);
getChildren().addAll(Tpanel,Bpanel,label);
HBox.setMargin(Tpanel, new Insets(5,10,5,10));
HBox.setMargin(Bpanel, new Insets(5,10,5,10));
HBox.setMargin(label, new Insets(150,10,5,10));
}
}
public static void main(String[] args) {
launch(args);
}
}
Your MainPanel is a BorderPane: simply adding nodes to it will put them all in the top left by default (so they are all on top of each other). When using a BorderPane, you need to call setCenter(...), setTop(...), setLeft(...) etc to position the nodes.
E.g.:
// getChildren().addAll(Tpanel,Bpanel,label);
setTop(Tpanel);
setBottom(Bpanel);
setCenter(label);

Java fx Image path

i have a problem with javafx' ImageView.
I try to load a simple jpg, as an image and add it to an ImageView.
But Javafx can't finde the resouce.
Image image = new Image("image.jpg");
ImageView iv1_1 = new ImageView();
iv1_1.setImage(image);
the exception i get is this one:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
it is crashing in the line, where the image is created.
Here is a picture of the Project:
enter link description here
(I am not allowed to post pictures.)
Use getClass().getResource("image.jpg)
Please find below example to load image using JavaFX.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LoadImage extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Load Image");
StackPane sp = new StackPane();
Image img = new Image("javafx.jpg");
ImageView imgView = new ImageView(img);
sp.getChildren().add(imgView);
//Adding HBox to the scene
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Create one source folder with name Image in your project and add your image to that folder otherwise you can directly load image from external URL like following.
Image img = new Image(
"http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg"
);
this worked for me
Image image = null;
try {
image = new Image(new FileInputStream("image.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ImageView view = new ImageView();
view.setImage(image);

JavaFX 8 - How to wrap text in new line in several controls?

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.

Resources